0
public class TimeToDieThread extends Thread implements Runnable
{
private Runnable r;
private long lTimeLength;//time NanoTime
public TimeToDieThread(Runnable r, long lTimeLength)
{
super();
this.r = r;
this.lTimeLength = lTimeLength;
}
public void start() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
boolean bran = false;
while ((!Thread.currentThread().isInterrupted()) && (bran == false)) {
r.run();
bran = true;
}
}
});
t.start();
// Sleep a for entire length, and then interrupt
try
{
Thread.sleep(lTimeLength);
} catch (InterruptedException e)
{
System.out.println("Interrupted, wop wop waa");
}
t.interrupt();
}
}
'r.run()'不会被这种方式中断,否。在运行while循环之前't.run()'可能会被打断(它只会循环一次,错字?)。 –