使主线程等待其他线程完成

时间:2020-06-02 14:58:59

标签: java multithreading

我有以下Switch类,基本上是toggles isOn

我主要要制作thread1thread2,启动它们, 然后睡觉5秒钟,然后interrupt他们,我想我做对了

除了主线程应该等待两个线程都完成,这就是我添加的

thread1.join()
thread2.join()

但是这导致线程永远运行并且没有抛出异常,我该怎么办?还是main已经在等待他们完成?

public class Switch implements Runnable {
    private static boolean isOn;
    private String name;
    private static final Object lockedObject = new Object();

    public Switch(String name) {
        this.name = name;
    }

    public void toggle() {
        System.out.println(name + ": " + isOn);
        synchronized (lockedObject) {
            isOn = !isOn;
        }
    }

    @Override
    public void run() {
        while (true) {
            if (Thread.currentThread().isInterrupted()) {
                break;
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
                break;
            }
            synchronized (lockedObject) {
                toggle();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Switch switch1 = new Switch("switch1");
        Switch switch2 = new Switch("switch2");
        Thread thread1 = new Thread(switch1);
        Thread thread2 = new Thread(switch2);
        thread1.start();
        thread2.start();
//        thread1.join();
//        thread2.join();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread1.interrupt();
        thread2.interrupt();
    }
}

1 个答案:

答案 0 :(得分:2)

您需要将以下行放入catch块中:

Thread.currentThread().interrupt();

此外,您不需要以下几行:

if (Thread.currentThread().isInterrupted()) {
    break;
}

演示:

public class Switch implements Runnable {
    private static boolean isOn;
    private String name;
    private static final Object lockedObject = new Object();

    public Switch(String name) {
        this.name = name;
    }

    public void toggle() {
        System.out.println(name + ": " + isOn);
        synchronized (lockedObject) {
            isOn = !isOn;
        }
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();// Add this line
                break;
            }
            synchronized (lockedObject) {
                toggle();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Switch switch1 = new Switch("switch1");
        Switch switch2 = new Switch("switch2");
        Thread thread1 = new Thread(switch1);
        Thread thread2 = new Thread(switch2);
        thread1.start();
        thread2.start();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread1.interrupt();
        thread2.interrupt();
    }
}