如果java中存在未知数量的线程,如何通知所有线程

如果java中存在未知数量的线程,如何通知所有线程,java,Java,这是我的java代码的一部分。在这段代码中,有一些标签从0开始计数,以此类推 第一次单击按钮时,我想停止标签计数,第二次单击按钮时,我想重新启动标签计数。问题是,当我第二次单击按钮时,标签没有重新开始计数。那么,请告诉我,我应该如何通知所有标签重新开始计数 import java.awt.Color; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListene

这是我的java代码的一部分。在这段代码中,有一些标签从0开始计数,以此类推

第一次单击按钮时,我想停止标签计数,第二次单击按钮时,我想重新启动标签计数。问题是,当我第二次单击按钮时,标签没有重新开始计数。那么,请告诉我,我应该如何通知所有标签重新开始计数

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingWorker;
public class Main implements ActionListener {
    JButton button = new JButton("Click");
    JFrame frame = new JFrame();
    boolean wait=false;

    public static void main(String arg[]) {
        new Main();
    }

    public Main() {
        frame.setLayout(new FlowLayout());
        frame.getContentPane().setBackground(Color.BLACK);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        button.addActionListener(this);
        frame.add(button);
        frame.setVisible(true);
        new Producer().execute();
    }

    public class Producer extends SwingWorker<Void, Void> {
        public Void doInBackground() {
            for(int infinite=0; infinite!=-1; infinite++) {
                new Counter().execute();
                try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}     
            }
            return null;
        }
    }

    public class Counter extends SwingWorker<Void, Void> {
        JLabel label = new JLabel();
        public Counter() {
            label.setForeground(Color.WHITE);
            frame.add(label);
        }

        public Void doInBackground() {
            synchronized (this) {
                for(int i=0; i!=-1; i++) {
                    if(wait==true)
                        try {this.wait();} catch(Exception exp) {exp.printStackTrace();}
                    label.setText(""+i);
                    try {Thread.sleep(200);} catch (InterruptedException e) {e.printStackTrace();}      
                }
            }
            return null;
        }
    }

    public void actionPerformed(ActionEvent clicked) {
        if(wait==false)
            wait=true;
        else if(wait==true) {
            synchronized (this) {
                this.notifyAll();
            }
            wait=false;
        }
    }
}
导入java.awt.Color;
导入java.awt.FlowLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.SwingWorker;
公共类Main实现ActionListener{
JButton按钮=新JButton(“单击”);
JFrame=新JFrame();
布尔等待=假;
公共静态void main(字符串arg[]){
新的Main();
}
公用干管(){
frame.setLayout(新的FlowLayout());
frame.getContentPane().setBackground(颜色:黑色);
setExtendedState(JFrame.MAXIMIZED_二者);
addActionListener(这个);
框架。添加(按钮);
frame.setVisible(true);
新生产者().execute();
}
公共类制作人扩展SwingWorker{
公共无效doInBackground(){
for(int infinite=0;infinite!=-1;infinite++){
新计数器().execute();
尝试{Thread.sleep(1000);}catch(InterruptedException e){e.printStackTrace();}
}
返回null;
}
}
公共类计数器扩展SwingWorker{
JLabel标签=新的JLabel();
公众柜位(){
标签。设置前景(颜色。白色);
框架。添加(标签);
}
公共无效doInBackground(){
已同步(此){
对于(int i=0;i!=-1;i++){
if(wait==true)
尝试{this.wait();}catch(异常exp){exp.printStackTrace();}
label.setText(“+i”);
尝试{Thread.sleep(200);}catch(InterruptedException e){e.printStackTrace();}
}
}
返回null;
}
}
已执行公共作废操作(单击操作事件){
if(wait==false)
等待=真;
else if(wait==true){
已同步(此){
this.notifyAll();
}
等待=错误;
}
}
}

this.notifyAll()
中的
this
this.wait()
中的
this
不是同一个对象。在我看来,您好像是在从EDT之外访问和变异Swing组件。这直接违反了Swing的线程策略,我强烈建议您在编写更多代码之前阅读Swing编程教程。为什么不设置一个单独的Count对象,一个线程,它将增加您想要的所有计数器。。。调用计数以注册一个新的计数(并返回一个id),然后调用获取值。没有意义启动所有这些线程。。线程是昂贵的!“停止标签计数”不进行分析。如何“停止”标签,标签是在计数还是你在计数?当然,没有办法任意停止其他线程。你所能做的就是与其他线程会合并交换信息。关于同一个程序,你问了三个问题?