Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中停止、暂停、取消线程_Java_Multithreading - Fatal编程技术网

如何在java中停止、暂停、取消线程

如何在java中停止、暂停、取消线程,java,multithreading,Java,Multithreading,我正在用java开发一个应用程序,它启动一些线程来完成一些工作,并用JProgressBar更新JTable。 我在JTable上开发了一个jpopmpmenu,其中包含一些JMenuItem: 停顿 停止 取消 恢复 所以我希望能够做到这一点 当用户在JTable中添加新线程时,我将线程保存在ArrayList中,因此我必须实现 stop.addActionListener(new ActionListener() { @Override p

我正在用java开发一个应用程序,它启动一些线程来完成一些工作,并用
JProgressBar
更新
JTable
。 我在
JTable
上开发了一个
jpopmpmenu
,其中包含一些
JMenuItem

  • 停顿
  • 停止
  • 取消
  • 恢复
所以我希望能够做到这一点

当用户在
JTable
中添加新线程时,我将线程保存在
ArrayList
中,因此我必须实现

stop.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {

            }
        });
另一个

所以我尝试,假设我有当前线程的索引:

Thread t = working.get(selectedThread); //where working is my `ArrayList<Thread>`
t.interrupt();
但是它在
wait()
上给我带来了
IllegalStateMonitorException
,所以我不知道怎么办。。有人能帮我吗?

调用
线程.interrupt()
只设置
线程上的中断位,它会导致任何
等待
睡眠
调用抛出
中断异常
。它不会像许多人期望的那样取消线程的执行

您可以在如下线程中测试中断位:

while (!Thread.currentThread().isInterrupted()) {
   ...
}
人们停止线程的一种典型方法是使用
原子布尔值(或
可变布尔值)并执行以下操作:

AtomicBoolean running = new AtomicBoolean(true);
while (running.set()) {
    ...
}

...
// then in the other thread (like the main thread) you stop the thread by:
runner.set(false);
Thread t = working.get(actualRow);
synchronized (t) {
    t.wait();
}
您得到的是
IllegalStateMonitorException
,因为您正在调用
wait
,而不在正在等待的对象的
synchronized
块中。您需要执行以下操作:

AtomicBoolean running = new AtomicBoolean(true);
while (running.set()) {
    ...
}

...
// then in the other thread (like the main thread) you stop the thread by:
runner.set(false);
Thread t = working.get(actualRow);
synchronized (t) {
    t.wait();
}
虽然我不确定那是你想要的。也许你想加入等待它完成的线程

   working.get(actualRow).join();

IllegalStateMonitorException
是因为线程只能在它“拥有”的对象(我不记得它是否是正确的术语)中等待它

您需要首先通过同一对象进行同步,以确保没有其他人正在等待此对象

 synchronize (working.get(actualRow)) {
   working.get(actualRow).wait(); 
 }

问题是我的i-thread(我的意思是有很多线程一起工作)的任务是解析两个innested循环中的某个html页面。那么我可以把你的情况放在哪里?杰克的目标是什么?您正在尝试停止此html页面的处理吗?很明显,你可以每隔一段时间做一次if。不。。我解释得不好。。我有一个Jpanel,其中有一个JTable和一个JComboBox。用户可以在JCombobox中选择一个对象,然后单击JButton可以启动一个线程。这个线程首先更新JTable,在JTable中添加一行,其中包含一些文本和一个JProgressBar(由刚刚启动的线程操作),然后在每个页面上解析许多html页面更改,在JTable中插入的行中包含一些文本和JProgressBar的值。用户可以启动多个线程,然后用鼠标右键单击一行JTable,显示我的帖子中描述的JPOPUPMENUS。使用JPOPUPMENUS,用户可以停止、暂停、取消恢复与单击的行相关联的线程。@Jack就是这个线程,或者它正在等待某个条件。如果它正在等待,那么您应该捕获
InterruptedException
,然后退出线程。你是不是抓住并忽略了其中的一个?对不起。。它会阻止所有GUI,只有一个线程可以位于给定对象的同步部分。如果在另一个线程尝试与同一对象同步的对象中执行同步,则第二个线程将停止,直到第一个线程离开同步块。这正是同步的目的。如果您只想在不阻塞问题的情况下执行等待,请使用不同的对象(新创建的对象,甚至是自己的线程对象)同步每个线程。您需要锁定working.get(actualRow)对象才能调用等待。这就是你得到非法状态监视异常的原因。可以从synchronized块或synchronized方法调用wait。不要在线程上使用中断调用,而是通过设置线程的某些变量向线程发送通知。在线程中,使用此值停止执行