問題:執行緒的終止
public class Squasher implements Runnable {
Buffer buf1;
Buffer buf2;
char ch = ' ';
boolean hold = true;
/*
* constructor: make a connection between buffer1 and buffer2, both of them
* are related to the squsher class
*/
public Squasher(Buffer buf1, Buffer buf2) {
// TODO Auto-generated constructor stub
this.buf1 = buf1;
this.buf2 = buf2;
(new Thread(this, "squasher")).start();
}
public void run() {
char tmpch;
// TODO Auto-generated method stub
//
while (!Thread.currentThread().isInterrupted()) {
if (ch == '\0') {
try {
buf2.put(ch); //write the char into buf2
Thread.currentThread().interrupt();
//here the thread does not terminate, but i don't know why
//為什麼在這個地方執行緒不終止
break;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
try {
ch = buf1.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//change the * into ^
if (ch == '*') {
try {
tmpch = buf1.get();
if (tmpch == '*') {
buf2.put('^');
} else {
buf2.put('*');
buf2.put(tmpch);
}
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
try {
buf2.put(ch); //write the char into buf2
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
Buffer buf1;
Buffer buf2;
char ch = ' ';
boolean hold = true;
/*
* constructor: make a connection between buffer1 and buffer2, both of them
* are related to the squsher class
*/
public Squasher(Buffer buf1, Buffer buf2) {
// TODO Auto-generated constructor stub
this.buf1 = buf1;
this.buf2 = buf2;
(new Thread(this, "squasher")).start();
}
public void run() {
char tmpch;
// TODO Auto-generated method stub
//
while (!Thread.currentThread().isInterrupted()) {
if (ch == '\0') {
try {
buf2.put(ch); //write the char into buf2
Thread.currentThread().interrupt();
//here the thread does not terminate, but i don't know why
//為什麼在這個地方執行緒不終止
break;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
try {
ch = buf1.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//change the * into ^
if (ch == '*') {
try {
tmpch = buf1.get();
if (tmpch == '*') {
buf2.put('^');
} else {
buf2.put('*');
buf2.put(tmpch);
}
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
try {
buf2.put(ch); //write the char into buf2
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
相關文章
- 執行緒的基本操作:新建和終止執行緒執行緒
- Thread執行緒終止interruptthread執行緒
- 終止java執行緒的2種方法Java執行緒
- Java之 join 等待執行緒終止Java執行緒
- 如何"優雅"地終止一個執行緒?執行緒
- 併發程式設計——如何終止執行緒程式設計執行緒
- Thinking in Java---如何正確的終止子執行緒ThinkingJava執行緒
- Java實現終止執行緒池中正在執行的定時任務Java執行緒
- ArrayList 的執行緒安全問題執行緒
- 03 執行緒安全問題執行緒
- SimpleDateFormat 執行緒安全問題ORM執行緒
- parallelStream中的執行緒安全問題Parallel執行緒
- 終止expdp正在執行中的匯出任務
- Java多執行緒中執行緒安全與鎖問題Java執行緒
- 執行緒中斷以及執行緒中斷引發的那些問題執行緒
- 子執行緒使用父執行緒RequestScope作用域Bean問題的探究執行緒Bean
- subprocess.Popen 如何提前終止程式執行
- 多執行緒相關問題執行緒
- 多執行緒問題解釋執行緒
- JUC之多執行緒鎖問題執行緒
- 深入JAVA執行緒安全問題Java執行緒
- 模板方法中的執行緒安全問題執行緒
- lambda中stream執行緒安全的問題執行緒
- 第15篇 執行緒鎖的問題執行緒
- Java 執行緒安全問題的本質Java執行緒
- 多執行緒引起的效能問題分析執行緒
- drools執行完某個規則後終止別的規則執行
- 多執行緒,你覺得你安全了?(執行緒安全問題)執行緒
- iOS多執行緒全套:執行緒生命週期,多執行緒的四種解決方案,執行緒安全問題,GCD的使用,NSOperation的使用iOS執行緒GC
- 多執行緒之8鎖問題執行緒
- 05.java多執行緒問題Java執行緒
- 子執行緒 UI 問題捉蟲執行緒UI
- SpringMVC中出現的執行緒安全問題分析SpringMVC執行緒
- 多執行緒的安全性問題(三)執行緒
- 關於js執行緒問題的解讀JS執行緒
- ThreadLocal執行緒重用時帶來的問題thread執行緒
- JUC之集合中的執行緒安全問題執行緒
- Spring中多執行緒的使用及問題Spring執行緒
- Java多執行緒面試高配問題---多執行緒(3)🧵Java執行緒面試