问题:线程的终止

05-12-17 hti
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();

}

}

}

}

}

}

banq
2005-12-21 09:54
不是使用Thread.currentThread().interrupt()来停止线程吧?具体语法已经忘记。你查查。

猜你喜欢