简体   繁体   English

java线程在运行多个线程时不会中断

[英]java thread not interrupt when running many thread

My project run on java swing there are 2 button (start/stop) for counting. 我的项目在java swing上运行,有2个按钮(开始/停止)进行计数。

When I click start button. 当我点击开始按钮。 There is one thread running (Thread-0) ,then click stop button "Thread-0" disappear but when I click start button many times. 有一个线程正在运行(Thread-0),然后单击停止按钮“Thread-0”消失,但是当我多次单击开始按钮时。 there are many threads such as Thread-5, Thread-6, .. Thread-10 are running. 有许多线程,如Thread-5,Thread-6,.. Thread-10正在运行。

Problem : If click start and then stop counting is ok. 问题:如果单击开始然后停止计数就可以了。 but click start many times it's incorrect counting. 但点击开始多次计数不正确。

start button 开始按钮

private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {         
    start();
    btnStart.setEnabled(false);
    btnStop.setEnabled(true);
}   

stop button 停止按钮

private void btnStopActionPerformed(java.awt.event.ActionEvent evt) {            
    isEnable = false;
    btnStop.setEnabled(false);
    btnStart.setEnabled(true);
}    

start() method : start()方法:

isEnable = true;
Thread refreshPlan = new Thread() {  
    @Override
    public void run() {
        while(isEnable) {
            try {
                sleep(CYCLE_TIME * 1000);
                PLAN += 1;
                planValue.setText(String.valueOf(PLAN));
            } catch (InterruptedException ex) {
                //ignore
            }
        }
    };
    };
    refreshPlan.start();

Can I run only single thread when click many times in start button ? 在开始按钮中多次单击时,我可以只运行单个线程吗? any suggestion? 有什么建议吗? thanks. 谢谢。

sorry for my bad english. 对不起,我的英语不好。

There are four significant problems here: 这里有四个重要问题:

  • Unless isEnable is declared as a volatile variable, there's no guarantee that a write from one thread will be seen in another thread 除非将isEnable声明为volatile变量,否则无法保证在另一个线程中可以看到来自一个线程的写入
  • Likewise your access to the PLAN counter (which is badly named - please follow Java naming conventions) is unsafe. 同样,您访问PLAN计数器(名称错误 - 请遵循Java命名约定)是不安全的。 You may wish to consider using AtomicInteger for this. 您可能希望考虑使用AtomicInteger
  • You're making changes to the UI from your extra thread. 您正在从额外的线程更改UI。 You can't do that - in Swing (and most UIs) all access to UI components has to be done on the thread responsible for that UI. 你不能这样做 - 在Swing(和大多数UI)中,必须在负责该UI的线程上完成对UI组件的所有访问。 See the Swing concurrency tutorial for more details. 有关更多详细信息,请参阅Swing并发教程
  • Because you only check isEnabled one per second, it would be possible to stop and start multiple threads in the meantime... leading to several threads being active at once. 因为您每秒只检查一次isEnabled ,所以可以在此期间停止并启动多个线程...导致多个线程同时处于活动状态。 This could interfere with your counting. 这可能会干扰您的计数。

You might find it's better to use a javax.swing.Timer which fires once a second, and just checks whether or not it's meant to do anything. 您可能会发现最好使用每秒触发一次的javax.swing.Timer ,并检查它是否意味着要做任何事情。 That way everything can be on the UI thread. 这样一切都可以在UI线程上。

start method is instantiating new Thread() that's why every time u click it, it brings new thread into life. start方法是实例化新的Thread(),这就是为什么每次你点击它,它都会带来新的线程。

declare Thread refreshPlan class variable then in start method put all the code in this check 声明Thread refreshPlan类变量然后在start方法中将所有代码放入此检查中

if(refreshPlan == null || !refreshPlan.isAlive()){
//ur existing code to instantiate new thread.
}

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

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