有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中多线程处理我的线程

尝试使用线程执行简单的程序。但有些事情我不明白。这是我的代码:

 import javax.swing.*;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;

public class MainClass {
    public static void main(String[] args) {
    new BIGBUTTON();

}}

class GUILCLASS extends Thread {
int i;
public GUILCLASS(){
    start();
    i=0;
}

@Override
public void run() {
    super.run();
    while (true){
        System.out.println("I did this cycle " +i +" times");
        i++;
        try {
            sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();  
        }
    }
 }
 }
   class BIGBUTTON extends JFrame {
private JButton buttonForTestingButton;
private JPanel panel1;
GUILCLASS guilclass=new GUILCLASS();
boolean mark1;

public BIGBUTTON() {
    panel1=new JPanel();
    buttonForTestingButton=new JButton("Button for testing");
    mark1=true;
    setVisible(true);
    setBounds(100,100,100,100);
    add(panel1);
    add(buttonForTestingButton);
    buttonForTestingButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            super.mouseClicked(e);
            if(mark1){
                synchronized (guilclass){
                    try {
                        guilclass.wait();
                        mark1=false;
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();  
                    }
                }
            } else {
                synchronized (guilclass){
                    guilclass.notify();
                    mark1=true;
                }
            }
        }
    });
     }
  }

我想做的是点击按钮,让一个线程等待()直到我再次点击这个按钮,但我的按钮只是冻结了,其他什么都没有发生。正在寻求帮助


共 (0) 个答案