[英]How to stop running threads when the conditions are met?
I would like some suggestions on how to stop the threads once the conditions are met. 我想要一些有关在条件满足后如何停止线程的建议。
Here are my codes: 这是我的代码:
public class Animal implements Runnable {
private static boolean winner = false;
private String name;
private int position;
private int speed;
private long restMax;
public Animal(String name, int position, int speed, long restMax) {
this.name = name;
this.position = position;
this.speed = speed;
this.restMax = restMax;
}
@Override
public void run() {
while (position < 100){
position += speed;
try {
Thread.sleep((long) (0 + (Math.random() * restMax)));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name + " is at position " + position );
}
if (position >= 100){
winner = true;
System.out.println(name + " is the winner of the race!");
Thread.interrupted();
}
}
}
here is the output for 2 Animal objects I created s in the main method: 这是我在main方法中创建的2个Animal对象的输出:
The threads kept on going even when the winner was declared (Homegrown Rabbit is the winner in this case). 即使宣布获胜者,线程仍在继续(在这种情况下,本地兔是获胜者)。 My question is how do I stop the other threads when the conditions are met? 我的问题是,在满足条件时如何停止其他线程? I tried the interrupted() method but I couldn't see any differences. 我尝试了interrupted()方法,但看不到任何区别。
Homegrown Rabbit is at position 5
Homegrown Rabbit is at position 10
Wild Turtle is at position 3
Wild Turtle is at position 6
Homegrown Rabbit is at position 15
Homegrown Rabbit is at position 20
Wild Turtle is at position 9
Wild Turtle is at position 12
Wild Turtle is at position 15
Homegrown Rabbit is at position 25
Wild Turtle is at position 18
Homegrown Rabbit is at position 30
Wild Turtle is at position 21
Homegrown Rabbit is at position 35
Wild Turtle is at position 24
Homegrown Rabbit is at position 40
Wild Turtle is at position 27
Wild Turtle is at position 30
Homegrown Rabbit is at position 45
Homegrown Rabbit is at position 50
Wild Turtle is at position 33
Homegrown Rabbit is at position 55
Wild Turtle is at position 36
Wild Turtle is at position 39
Homegrown Rabbit is at position 60
Wild Turtle is at position 42
Wild Turtle is at position 45
Homegrown Rabbit is at position 65
Wild Turtle is at position 48
Wild Turtle is at position 51
Homegrown Rabbit is at position 70
Homegrown Rabbit is at position 75
Wild Turtle is at position 54
Wild Turtle is at position 57
Wild Turtle is at position 60
Homegrown Rabbit is at position 80
Homegrown Rabbit is at position 85
Wild Turtle is at position 63
Wild Turtle is at position 66
Homegrown Rabbit is at position 90
Wild Turtle is at position 69
Wild Turtle is at position 72
Wild Turtle is at position 75
Homegrown Rabbit is at position 95
Wild Turtle is at position 78
Homegrown Rabbit is at position 100
Homegrown Rabbit is the winner of the race!
Wild Turtle is at position 81
Wild Turtle is at position 84
Wild Turtle is at position 87
Wild Turtle is at position 90
Wild Turtle is at position 93
Wild Turtle is at position 96
Wild Turtle is at position 99
Wild Turtle is at position 102
Wild Turtle is the winner of the race!
You don't need any Thread primitives to stop the thread. 您不需要任何Thread原语即可停止线程。 You just need to exit the while loop. 您只需要退出while循环。
Here is a modified run 这是修改后的运行
public void run()
{
while (winner == false)
{
position += speed;
System.out.println(name + " is at position " + position );
if(position >= 100)
{
winner = true;
System.out.println(name + " is the winner of the race!");
break;
}
try
{
Thread.sleep((long) (0 + (Math.random() * restMax)));
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
If winner is found, simply exit the loop. 如果找到赢家,只需退出循环。
@Override
public void run() {
while (position < 100){
position += speed;
try {
Thread.sleep((long) (0 + (Math.random() * restMax)));
} catch (InterruptedException e) {
e.printStackTrace();
}
/* Since winner is static, once one thread sets this to true,
other threads will stop their execution as well. */
if (winner)
break;
System.out.println(name + " is at position " + position );
}
I executed this one and got this: 我执行了这个并得到了:
rabbit is at position 2
horse is at position 5
rabbit is at position 4
horse is at position 10
horse is at position 15
horse is at position 20
horse is at position 25
horse is at position 30
rabbit is at position 6
horse is at position 35
rabbit is at position 8
horse is at position 40
horse is at position 45
horse is at position 50
horse is at position 55
horse is at position 60
horse is at position 65
horse is at position 70
horse is at position 75
horse is at position 80
horse is at position 85
horse is at position 90
horse is at position 95
horse is at position 100
horse is the winner of the race!
Check the condition in every loop,when met,break out that loop,and the thread will be stopped. 检查每个循环中的条件,如果满足,请中断该循环,然后线程将停止。
interrupt()
a thread is just to set the interrupt flag,nothing else changed,you can check that value by isInterrupted()
interrupt()
一个线程只是用来设置中断标志,其他都没有改变,可以通过isInterrupted()
检查该值
generally speaking,a thread is unable to stop unless itself wish to. 一般而言,除非自己希望,否则线程无法停止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.