問題:執行緒的終止
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();
}
}
}
}
}
}
相關文章
- 執行緒的基本操作:新建和終止執行緒執行緒
- Java執行緒中斷與終止執行緒執行Java執行緒
- Thread執行緒終止interruptthread執行緒
- 終止java執行緒的2種方法Java執行緒
- 如何正確終止正在執行的子執行緒執行緒
- Java之 join 等待執行緒終止Java執行緒
- Posix執行緒程式設計指南(4)-執行緒終止 (轉)執行緒程式設計
- Java關閉Socket來終止執行緒Java執行緒
- 併發程式設計——如何終止執行緒程式設計執行緒
- 如何"優雅"地終止一個執行緒?執行緒
- Thinking in Java---如何正確的終止子執行緒ThinkingJava執行緒
- Java實現終止執行緒池中正在執行的定時任務Java執行緒
- Java 終止執行緒方法Java執行緒
- javascript如何終止函式的執行JavaScript函式
- 執行緒問題執行緒
- Java多執行緒設計模式(6)兩階段終止模式Java執行緒設計模式
- java多執行緒執行問題Java執行緒
- 多執行緒問題執行緒
- ArrayList 的執行緒安全問題執行緒
- 關於執行緒的問題...執行緒
- 多執行緒鎖的問題執行緒
- 終止expdp正在執行中的匯出任務
- oracle 如何終止儲存過程的執行Oracle儲存過程
- SimpleDateFormat 執行緒安全問題ORM執行緒
- java執行緒安全問題Java執行緒
- subprocess.Popen 如何提前終止程式執行
- parallelStream中的執行緒安全問題Parallel執行緒
- 所謂的執行緒安全問題執行緒
- 執行緒池的設計問題執行緒
- 多執行緒-執行緒安全問題的產生原因分析以及同步程式碼塊的方式解決執行緒安全問題執行緒
- Java多執行緒中執行緒安全與鎖問題Java執行緒
- 多執行緒程式設計,處理多執行緒的併發問題(執行緒池)執行緒程式設計
- 終止imp/exp和expdp/impdp程式執行的方法
- 執行緒中斷以及執行緒中斷引發的那些問題執行緒
- 子執行緒使用父執行緒RequestScope作用域Bean問題的探究執行緒Bean
- 多執行緒下的程式同步(執行緒同步問題總結篇)執行緒
- Java多執行緒面試高配問題---多執行緒(3)🧵Java執行緒面試
- 多執行緒問題解釋執行緒