为什么我不能停止/终止正在运行的线程并能够暂停和继续。我有一个类MyThreadProcessor
实现了Runnable
接口:
public class MyThreadProcessor implements Runnable {
private volatile boolean running = true;
private volatile boolean paused = false;
private List<String> pauseLock = new ArrayList<>();
@Override
public void run() {
while (running) {
synchronized (pauseLock) {
if (!running) {
break;
}
if (paused) {
try {
pauseLock.wait();
} catch (InterruptedException ex) {
break;
}
if (!running) { // running might have changed since we paused
break;
}
}//if(paused)
}//syncronized
try {
System.err.println("Sleeping...");
Thread.sleep((long) 5000);
System.err.println("Processing");
} catch (InterruptedException e) {
System.err.println("Exception"+ e);
running = false;
}
}//while
}//run()
public void terminate() {
running = false;
}
public void pause() {
paused = true;
}
public void resume() {
synchronized (pauseLock) {
paused = false;
pauseLock.notifyAll(); // Unblocks thread
}
}
}
和MyTreadInitializer类,如
public class MyTreadInitializer {
private Thread thread = null;
private IndexProcessor runnable = null;
public void startMyTread() {
runnable = new IndexProcessor();
thread = new Thread(runnable);
System.err.println("Starting thread: " + thread);
thread.start();
System.err.println("Background process successfully started.");
}
public void stopMyTread() {
System.err.println("Stopping thread: " + thread);
if (thread != null) {
runnable.terminate();
try {
thread.join();
} catch (InterruptedException e) {
System.err.println("Exception In Main"+ e);
e.printStackTrace();
}
System.err.println("Thread successfully stopped.");
}
}
public void pauseMyTread() {
System.err.println("Pausing thread: " + thread);
if (thread != null) {
runnable.pause();
System.err.println("Thread successfully Paused.");
}
}
public void resumeMyTread() {
System.err.println("Resume thread: " + thread);
if (thread != null) {
runnable.resume();
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.err.println("Exception In Pause"+ e);
e.printStackTrace();
}
System.err.println("Thread successfully stopped.");
}
}
}
主要类别是
public class MyMainTH {
public static void main(String[] args) {
MyTreadInitializer mi = new MyTreadInitializer();
try {
mi.startMyTread();
Thread.sleep(15000);
mi.pauseMyTread();
System.out.println("After Pause before Sleep");
Thread.sleep(15000);
System.out.println("After Sleep before Resume");
mi.resumeMyTread();
System.out.println("After resume before sleep");
Thread.sleep(15000);
System.out.println("After resume After sleep before Stop");
mi.stopMyTread();
} catch (InterruptedException e) {
System.err.println("In Main Catch: " + e);
e.printStackTrace();
}
}
}
在这里我得到类似的输出
启动线程:线程[Thread-0,5,main]
成功启动了后台进程。
睡觉...
处理
睡觉...
处理
睡觉...
暂停线程:线程[Thread-0,5,main]
在暂停后之前
SleepThread已成功暂停。
处理
在睡眠后恢复之前
恢复线程:线程[Thread-0,5,main]
睡觉...
处理
睡觉...
正在处理............
Continue it is
not run mi.stopMyTread()
;
任何解决方法..谢谢。
答案 0 :(得分:0)
当我刚刚删除它时就可以使用
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.err.println("Exception In Pause"+ e);
e.printStackTrace();
}
来自resumeMyThread()
,就可以了。谢谢@RealSkeptic