简体   繁体   中英

make main thread wait for other threads to finish

I have the following Switch class that basically toggles isOn

what i want to in main is make thread1 and thread2 , start them, then sleep for 5 seconds, and after that interrupt them, i think i did this the correct

aside from main thread should wait for both threads to finish , that's where i added

thread1.join()
thread2.join()

but that leads to the threads running forever and no exception thrown, how should i do that? or is main already waiting for them to finish?

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();
    }
}

You need to put the following line in the catch block:

Thread.currentThread().interrupt();

Also, you do not need the following lines:

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

Demo:

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM