While loop not ending when flag changed in different thread [duplicate]

一笑奈何 提交于 2019-12-05 05:53:11

You need to declare the flag volatile, otherwise the compiler can optimize your code and skip the reads of the flag.

While the volatile solution that others proposed should work, unless you need the code in the while-loop to execute continuously, you should probably instead use wait() and notify() (or notifyAll()) inside of a synchronized section (to avoid "busy waiting"). Something like:

public class ThreadWhile implements KeyListener {
    private boolean flag = false;
    private Object flagLock = new Object();

    public static void main(String[] args) {
        //Set up key listening on a dummy JFrame
        JFrame frame = new JFrame("Key Detector 9000");
        frame.setVisible(true);
        ThreadWhile tw = new ThreadWhile();
        frame.addKeyListener(tw);

        System.out.println("Waiting until flag becomes true...");
        synchronized (tw.flagLock) {
            while(!tw.flag)
                tw.flagLock.wait();   // Note: this suspends the thread until notification, so no "busy waiting"
        }
        System.out.println("Flag is true, so loop terminated.");
        System.exit(0);
    }

    public void keyPressed(KeyEvent e) {
        synchronized (flagLock) {
            flag = true;
            System.out.println("flag: " + flag);
            flagLock.notifyAll();
        }
    }

    ...

Otherwise, you are repeatedly wasting cycles on the main thread checking the value of flag over and over (which, in general, if it happens enough, can slow down the CPU for other threads and may wear out the battery on mobile devices). This is exactly the sort of situation that wait() was designed for.

1. You need to use volatile keyword for the flag, Because when we call volatile on an Instance variable, then its like telling the JVM to make sure that the thread accessing it must reconcile its own copy of this instance variable with the one stored in the memory.

2. Volatile will also help in preventing the caching of the value in the thread.

3. Volatile will make that variable reflects its changed value in one thread to the another, but that does NOT prevent the race condition.

4. So use synchronized keyword on the Atomic statements which is setting the value of the flag.

Eg:

public void keyPressed(KeyEvent e) {
     synchronized(this){
        flag = true;
        System.out.println("flag: " + flag);
      }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!