【发布时间】:2018-12-17 22:17:33
【问题描述】:
为什么我无法停止/终止我正在运行的线程并且无法暂停和恢复。我有,一个类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[Thread-0,5,main]
后台进程成功启动。
睡觉……
处理
睡觉……
处理
睡觉……
暂停线程:Thread[Thread-0,5,main]
在暂停之后
SleepThread 已成功暂停。
处理
睡眠后恢复
恢复线程:Thread[Thread-0,5,main]
睡觉……
处理
睡觉……
处理中............
Continue it is
not run mi.stopMyTread();
任何解决它..谢谢。
【问题讨论】:
-
您正在加入您的
resumeMyTread中的线程。为什么? -
感谢您的回复,我正在使用加入我的线程进行 notifyAll()。
-
加入与
notifyAll无关。thread.join()表示“暂停当前线程(在您的情况下为主线程),直到thread停止运行。 -
糟糕,当我删除 `try { thread.join(); } catch (InterruptedException e) { // TODO 自动生成的 catch 块 System.err.println("Exception In Pause"+ e); e.printStackTrace(); }`。谢谢@RealSkeptic。
标签: java spring multithreading spring-boot