【面试手撕】多线程/并发编程
文章目录
- 前言
- 三个线程,交替打印A、B、C
- 两个线程1~100交替输出奇数和偶数
- 10个线程,每个线程+1w,最终变量到达10w
- 模拟死锁
- 让三个线程怎么串行执行
- 1.使用join方法
- 2.使用CountDownLatch
 
前言
本文总结面试中常考的手撕多线程问题。
三个线程,交替打印A、B、C
package com.fwedu.question_;
/**
 * 三个线程交替打印A\B\C
 */
public class Question3 {
    private static final Object LOCK = new Object();
    private static volatile int count = 0;
    private static final int MAX = 101;
    public static void main(String[] args) {
        new Thread(new Seq(0)).start();
        new Thread(new Seq(1)).start();
        new Thread(new Seq(2)).start();
    }
    static class Seq implements Runnable {
        private final int index;
        Seq(int index) {
            this.index = index;
        }
        @Override
        public void run() {
            while (count < MAX) {
                synchronized (LOCK) {
                    try {
                        while (count % 3 != index) {
                            LOCK.wait();
                        }
                        if (count <= MAX) {
                            System.out.println("Thread-" + index + ":" + (char)('A' + count % 3));
                        }
                        count++;
                        LOCK.notifyAll();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
两个线程1~100交替输出奇数和偶数
package com.fwedu.question_;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
 * 两个线程1~100交替输出奇数和偶数
 */
public class Question2 {
    private static CountDownLatch cl = new CountDownLatch(2);
    private static final Lock lock = new ReentrantLock();
    private static int cnt = 1;
    private static final int maxCnt = 100;
    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 2, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1));
        threadPoolExecutor.execute(() -> {
            while (cnt <= maxCnt) {
                lock.lock();
                try {
                    if (cnt <= maxCnt && cnt % 2 == 0) {
                        System.out.println(Thread.currentThread() + " " + cnt);
                        cnt++;
                    }
                } finally {
                    lock.unlock();
                }
            }
            cl.countDown();
        });
        threadPoolExecutor.execute(() -> {
            while (cnt <= maxCnt) {
                lock.lock();
                try {
                    if (cnt <= maxCnt && cnt % 2 != 0) {
                        System.out.println(Thread.currentThread() + " " + cnt);
                        cnt++;
                    }
                } finally {
                    lock.unlock();
                }
            }
            cl.countDown();
        });
        cl.await();
        threadPoolExecutor.shutdown();
    }
}
10个线程,每个线程+1w,最终变量到达10w
package com.fwedu.question_;
/**
 * 10个线程,每个线程+1w,最终变量到达10w
 */
public class Question1 {
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; ++i) {
            new Thread(new T()).start();
        }
        Thread.sleep(100);  // 确保上面的代码都执行完了
        System.out.println(T.val);
    }
}
class T extends Thread {
    static long val = 0;
    long cnt = 0;
    @Override
    public void run() {
        while (cnt < 100) {
            cnt++;
            synchronized (T.class) {
                val++;
            }
            System.out.println(Thread.currentThread() + " " + cnt + " " + val);
        }
    }
}
模拟死锁
package com.fwedu.syn;
/**
 * @author 冯威
 */
public class DeadLock {
    public static void main(String[] args) {
        //模拟死锁现象
        DeadLockDemo A = new DeadLockDemo(true);
        A.setName("A 线程");
        DeadLockDemo B = new DeadLockDemo(false);
        B.setName("B 线程");
        A.start();
        B.start();
    }
}
class DeadLockDemo extends Thread {
    static Object o1 = new Object();
    static Object o2 = new Object();
    boolean flag;
    public DeadLockDemo(boolean flag) {
        this.flag = flag;
    }
    @Override
    public void run() {
        if (flag) {
            synchronized (o1) {
                System.out.println(Thread.currentThread().getName() + " 进入 1");
                synchronized (o2) {
                    System.out.println(Thread.currentThread().getName() + " 进入 2");
                }
            }
        } else {
            synchronized (o2) {
                System.out.println(Thread.currentThread().getName() + " 进入 3");
                synchronized (o1) {
                    System.out.println(Thread.currentThread().getName() + " 进入 4");
                }
            }
        }
    }
}
让三个线程怎么串行执行
1.使用join方法
public class SerialExecutionUsingJoin {
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> {
            System.out.println("线程 1 执行");
        });
        Thread thread2 = new Thread(() -> {
            try {
                thread1.join();
                System.out.println("线程 2 执行");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread thread3 = new Thread(() -> {
            try {
                thread2.join();
                System.out.println("线程 3 执行");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        thread1.start();
        thread2.start();
        thread3.start();
    }
}
2.使用CountDownLatch
import java.util.concurrent.CountDownLatch;
public class SerialExecutionUsingCountDownLatch {
    public static void main(String[] args) {
        CountDownLatch latch1 = new CountDownLatch(1);
        CountDownLatch latch2 = new CountDownLatch(1);
        Thread thread1 = new Thread(() -> {
            System.out.println("线程 1 执行");
            latch1.countDown();
        });
        Thread thread2 = new Thread(() -> {
            try {
                latch1.await();
                System.out.println("线程 2 执行");
                latch2.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread thread3 = new Thread(() -> {
            try {
                latch2.await();
                System.out.println("线程 3 执行");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        thread1.start();
        thread2.start();
        thread3.start();
    }
}
