Skip to content

Commit

Permalink
更新子线程退出逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
bit4woo committed Apr 1, 2022
1 parent 6f9ffcd commit 4938245
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
13 changes: 11 additions & 2 deletions src/thread/Producer.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,19 @@ public void stopThread() {
public void run() {
while(true){
try {
if (domainQueue.isEmpty() || stopflag) {
//stdout.println(threadNo+" Producer exited");
if (domainQueue.isEmpty() ) {
stdout.println(getName()+" exited. due to domainQueue is empty");
break;
}
if (stopflag){
stdout.println(getName()+" exited. due to stop flag is true");
break;
}
if (Thread.interrupted()){
stdout.println(getName()+" exited. due to thread interrupt signal received");
break;
}

String host = domainQueue.take();
int leftTaskNum = domainQueue.size();

Expand Down
7 changes: 3 additions & 4 deletions src/thread/ThreadGetTitleWithForceStop.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,16 @@ public void run(){
//https://www.cnblogs.com/zheaven/p/12054044.html
} catch (InterruptedException e) {
stdout.println("force stop received");
for (Producer p:plist) {
p.interrupt();//由于当前主线程退出,并非是JVM退出,子线程还会继续运行,必须主动中断子线程才可以。
}
e.printStackTrace();
}

stdout.println("all producer threads finished");
AllProductorFinished = true;
}

boolean isAllProductorFinished(){
return AllProductorFinished;
}

@Deprecated
public void stopThreads() {
for (Producer p:plist) {
Expand Down

0 comments on commit 4938245

Please sign in to comment.