4Manuals

  • PDF Cloud HOME

中断可运行线程的问题 Download

    春季启动-Couchbase AbstractCouchbaseConfiguration-如何避免覆盖不必要的方法 我不知道(未解决的编译问题:) 加载数据并推送到未显示的RecyclerView中 调用Model类的函数时出现java.lang.NullPointerException 字符串数组在for循环中初始化时忽略索引0 Spring MVC @ModelAttribute未填充AJAX发布请求 需要建议在Java中使用哪个数据库/存储来存储RaftLogs(实现Raft Consensus Algorithm) Tensorflow多线程推理比单线程推理慢 Flutter:Java使用或覆盖已弃用的API @ EnableOAuth2Sso和@EnableResourceServer(同一应用程序中的客户端和资源行为)

我想用JFrame窗口运行一个类,等待它关闭,然后再启动另一个类,等等。(这应该是一个简单的登录窗口,随后会打开一个聊天窗口。)

此时,我正在努力中断正在运行登录窗口的线程。由于某种原因,无论我尝试什么,它都不会中断。这是基本代码:

public class Main {

public static void main(String[] args) throws InterruptedException {
    Account acc=new Account();  //create new account
    Thread t=new Thread(acc);   //put it in a thread
    a1.run();                   //start the thread
    while(t.isAlive()) {
        if(acc.success) {        //successful login
            t.interrupt();
        }
    }
    t.join();                   //wait for t to die
    System.out.println("Hello");
}

有关Account类的详细信息并不重要。它实现可运行,完成所有登录工作,如果成功,它将布尔成功更改为true,这将触发main中的中断。程序还应等待t被打断,然后打印“ Hello”,但不会。运行代码时,始终会立即打印出“ Hello”。

编辑:

public class Account extends JFrame implements Runnable {
JFrame window;
boolean success = false;

public Account() {
    super("Login");
}

@Override
public void run() {
    JButton login = new JButton("Login");
    window = new JFrame();
    window.setSize(500, 120);
    window.setLayout(new BorderLayout());

    window.add(login, BorderLayout.SOUTH);
    window.setVisible(true);
    login.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            checkAndFinish();
        }

    });

}

protected void checkAndFinish() {// no matter how, after checking the login the thread is supposed to close
    // Check if the login is correct, missing here for the sake of simplicity
    this.success = true;
    Thread.currentThread().interrupt();
}

}

这是帐户中的代码。它应该做的就是

if button pressed
kill this Thread

但它不像Thread.currentThread()。interrupt();

那样简单

0 个答案:

没有答案



Similar searches
    Opencart升级错误表定义不正确 我的统一应用程序的32位版本可以正常工作,但在64位版本的应用程序中会遇到很多麻烦 Swift:如何使用超类的枚举变量? 凯拉斯和浊点 如何在div标签中的Angular中打印此对象