問題:執行緒的終止

hti發表於2005-12-17
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();
}
}
}
}
}
}

相關文章