简体   繁体   English

修改EDT中的变量

[英]Modify variable in EDT

My loop is not stopping when i set the number[0] = ten, the thread for the while loop can't see the number being changed when the jbutton is pressed, How do i fixed this? 当我将数字设置为[0] = 10时,循环并没有停止,当按下jbutton时,while循环的线程看不到数字正在更改,我该如何解决? I believe the thread is being blocked. 我认为该线程已被阻止。

    public class A{

        private int[] number = new number[1];
        private number_game gui;


        public A(){
        }

        public void thread_loop(){

            gui = new number_game();
            gui.the_game(number);

            Thread nums = new Thread(){

                public void run(){

                    while(number[0]!= 10){

                           if(number != 10)
                              System.out.println("wrong number")
                    }

                };nums.start();

            } 
        }

    }

public class number_game extends Jframe{

......

    public number_game(){}

    / creating gui
    public void the_game(int [] guess){

       .............
       checkguess.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e){

                  guess[1] = 10;
                  dispose();

            }
        });
   }
}

public class main{
    public static void main(String[]args) {

    A nums_play = new A();
    nums_play.thread_loop();
    }

}

I'm hoping that this is just some threading practice. 我希望这只是一些线程练习。 To share values across threads you need to make sure that the access is synchronized properly, while there are a number of ways to do it, one of the simplest might be to use a AtomicInteger which takes care of all the multi-thread access functionality for you. 要在线程之间共享值,您需要确保访问已正确同步,尽管有多种方法可以实现,但最简单的方法之一可能是使用AtomicInteger来处理所有多线程访问功能。您。

I've also included a simple "lock" class which means that the Thread doesn't "free wheel" out of control and provides a means for the UI to alert the thread that a change has occurred, to which it can then process the value. 我还包括了一个简单的“锁”类,这意味着Thread不会“不受约束”,并为UI提供了一种方法来警告线程发生了更改,然后线程可以对其进行处理。值。

All this is shared across the thread monitor and UI classes. 所有这些都在线程监视器和UI类之间共享。

I strongly recommend having a look at the Concurrency Trail which covers this information in more detail 我强烈建议您查看“ 并发跟踪” ,其中详细介绍了此信息。

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test extends JFrame {

    public static void main(String[] args) {
        new Test();
    }

    public class CommonLock {

        private ReentrantLock lock;
        private Condition condition;

        public CommonLock() {
            lock = new ReentrantLock();
            condition = lock.newCondition();
        }

        protected void lock() {
            lock.lock();
        }

        protected void unlock() {
            lock.unlock();
        }

        public void await() throws InterruptedException {
            lock();
            try {
                condition.await();
            } finally {
                unlock();
            }
        }

        public void signal() {
            lock();
            try {
                condition.signal();
            } finally {
                unlock();
            }
        }

    }

    public Test() throws HeadlessException {
        CommonLock lock = new CommonLock();
        AtomicInteger value = new AtomicInteger(0);
        ThreadMonitor monitor = new ThreadMonitor(value, lock);
        monitor.start();

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane(value, lock));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private final AtomicInteger value;
        private final CommonLock lock;

        public TestPane(AtomicInteger value, CommonLock lock) {
            this.value = value;
            this.lock = lock;
            JButton btn = new JButton("Pick");
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    value.addAndGet(2);
                    lock.signal();
                }
            });
            add(btn);
        }

    }

    public class ThreadMonitor {

        private final AtomicInteger value;
        private final CommonLock lock;

        public ThreadMonitor(AtomicInteger value, CommonLock lock) {
            this.value = value;
            this.lock = lock;
        }

        public void start() {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            lock.await();
                        } catch (InterruptedException ex) {
                        }
                        int number = value.get();
                        if (number == 10) {
                            break;
                        } else {
                            System.out.println("Bad Guess");
                        }
                    }
                    System.out.println("Good guess");
                }
            });
            thread.start();
        }
    }
}

The example will increment the value (from 0 ) in steps of 2 , allow you to see the thread processing it until it reaches 10 该示例将以2为步长递增值(从0 ),让您看到正在处理它的线程,直到达到10

If you wanted to, you could create a single class which emcampasses the CommonLock and AtomicInteger , making the management slightly easier, but I'll leave that up to you 如果愿意,您可以创建一个包含CommonLockAtomicInteger ,使管理稍微容易一些,但我CommonLock您自己决定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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