问题描述
尝试构建一个 Runnable
必须在给定的时间限制内结束。
目前我正在使用 java.util.Timer
来中断 Runnable
线程。
计时器
在 Runnable
启动后立即启动。
import java.util.Timer;
import java.util.TimerTask;
公共类测试{
线程任务;
定时器计时器;
public void start(){
task = new Thread(new worker());
timer = new Timer(timer,true);
task.start();
timer.schedule(new Terminator(),1000 * 2);
}
类工人实现Runnable {
@覆盖
公共无效的run(){
为(INT I = 0; i< 2; i ++){
try {
Thread.sleep(1000);
} catch(InterruptedException e){
System.out.println(Test Interrupted !!);
返回;
}
}
System.out.println(Test Finished !!);
timer.cancel();
}
}
类终结者延伸的TimerTask {
@覆盖
公共无效的run(){
task.interrupt();
System.out.println(Test Terminated !!);
}
}
}
但我怀疑这导致计时器
和 Runnable
线程之间的竞争条件:
Runnable
即将完成,计时器
也在解雇。
这是否会导致完成!! 和终止!! 正在打印?如果是这样,人们如何避免它?
更新:
它发生:
11-09 21:14:40.741:INFO / System.out(31442):测试结束!! 18
11-09 21:14:40.751:INFO / System.out(31442):测试终止!! 18
11-09 21:14:40.751:INFO / System.out(31442):测试结束!! 19
11-09 21:14:40.751:INFO / System.out(31442):测试终止!! 19
是的。打印消息已完成时,计时器可能会在取消之前触发。
System.out.println( 完成!!);
//计时器可以在这里开火。
timer.cancel();
如果发生这种情况,将打印这两条消息。
为避免此问题,您可以使用同步。
synchronized( someObject){
if(!finished){
finished = true;
System.out.println(Finished !!);
timer.cancel();
}
}
在终止符中:
synchronized(someObject){
if(!finished){
finished = true;
task.interrupt();
System.out.println(Test Terminated !!);
}
}
这可以防止打印两个邮件。
Trying to construct a Runnable
that must end within a given time limit.
Currently I'm using java.util.Timer
to interrupt Runnable
thread.
Timer
starts right after Runnable
starts.
import java.util.Timer;
import java.util.TimerTask;
public class test {
Thread task;
Timer timer;
public void start(){
task = new Thread(new worker());
timer = new Timer("timer",true);
task.start();
timer.schedule(new Terminator(),1000*2);
}
class worker implements Runnable{
@Override
public void run() {
for (int i = 0 ; i < 2; i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Test Interrupted !!");
return;
}
}
System.out.println("Test Finished !!");
timer.cancel();
}
}
class Terminator extends TimerTask{
@Override
public void run() {
task.interrupt();
System.out.println("Test Terminated !!");
}
}
}
But I suspect this results in a race condition between Timer
and Runnable
threads:Runnable
is about to complete, and Timer
is also firing.
Can this ever result in both Finished !! and Terminated !! being printed ? If so, how one avoids it ?
Update:
It happens:
11-09 21:14:40.741: INFO/System.out(31442): Test Finished !! 18
11-09 21:14:40.751: INFO/System.out(31442): Test Terminated !! 18
11-09 21:14:40.751: INFO/System.out(31442): Test Finished !! 19
11-09 21:14:40.751: INFO/System.out(31442): Test Terminated !! 19
Yes. When the message "finished" is printed it's possible that the timer will fire before it is cancelled.
System.out.println("Finished !!");
// The timer can fire here.
timer.cancel();
If this happens, both messages will be printed.
To avoid the problem you can use synchronization.
synchronized (someObject) {
if (!finished) {
finished = true;
System.out.println("Finished !!");
timer.cancel();
}
}
And in the terminator:
synchronized (someObject) {
if (!finished) {
finished = true;
task.interrupt();
System.out.println("Test Terminated !!");
}
}
This prevents both messages form being printed.
这篇关于带有时间限制的Runnable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!