通过中断实现线程取消JAVA线程
class PrimrProducer extends Thread{
privatefinal BlockingQueue<BigInteger>queue;
PrimeProducer(BlockingQueue<BigInteger>queue){
this.queue=queue;
}
}
public void run(){
try{
BigInteger p=BigInteger.ONE;
while (!Thread.currentThread().isInterrupted())
queue.put(p=p.nextProbablePrime());
}
catch (InterruptedException consumed){
//允许线程退出
}
}
public voidcancel(){interrupt();}
}