面试白板题-Java多线程

401 阅读1分钟

1.用两个线程交替完成打印1-100的任务

public static void main(String[] args) {
    TestRunnable runnable = new TestRunnable();
    Thread thread1 = new Thread(runnable);
    Thread thread2 = new Thread(runnable);

    thread1.setName("线程1");
    thread2.setName("线程2");

    thread1.start();
    thread2.start();
}

private static class TestRunnable implements Runnable {
    int i = 1; // 使用runnable方便设置共享变量
    @Override
    public void run() {
        while(true) {
            synchronized (this) {
                notify();// 通知其他线程执行
                try {
                    Thread.sleep(50);
                    System.out.println(Thread.currentThread().getName() + ":" + i++);
                    wait(); // 将当前线程放到等待队列,等待调度
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(i > 100) break;
            }
        }
    }
}

2.生产者/消费者

public static void main(String[] args) {
    FactoryRunnable factoryRunnable = new FactoryRunnable();
    CarRunnable carRunnable = new CarRunnable();

    Thread factoryThread = new Thread(factoryRunnable);
    Thread carThread = new Thread(carRunnable);

    factoryThread.setName("工厂:");
    carThread.setName("汽车:");

    factoryThread.start();
    carThread.start();
}

private static final LinkedBlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>();

private static class FactoryRunnable implements Runnable {
    @Override
    public void run() {
        while(true) {
            synchronized (queue) {
                try {
                    if (queue.size() < 10) {
                        queue.offer((int)Math.floor(Math.random() * 100));
                        System.out.println(Thread.currentThread().getName() + " 库存剩余:" + queue.size());
                    } else {
                        queue.wait();
                    }
                    queue.notify();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

private static class CarRunnable implements Runnable {
    @Override
    public void run() {
        while(true) {
            synchronized (queue) {
                try {
                    if (queue.size() == 0) {
                        queue.wait();
                    } else {
                        queue.poll();
                        System.out.println(Thread.currentThread().getName() + " 库存剩余:" + queue.size());
                    }
                    queue.notify();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

3.现在有T1、T2、T3三个线程,怎样保证T2在T1执行完后执行,T3在T2执行完后执行?

public static void main(String[] args) {
    Runnable myRunnable = new MyRunnable();
    for(int i = 0; i < 3; i++) {
        Thread thread = new Thread(myRunnable);
        thread.start();
        try {
            thread.join();// join让当前线程等待子线程执行完再执行
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

private static class MyRunnable implements Runnable {
    private int i = 0;
    @Override
    public void run() {
        System.out.println("我是线程" + i++);
    }
}