java - How to stop running threads when the conditions are met? -
i suggestions on how stop threads once conditions met.
here 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 + " @ position " + position ); } if (position >= 100){ winner = true; system.out.println(name + " winner of race!"); thread.interrupted(); } } } here output 2 animal objects created s in main method:
the threads kept on going when winner declared (homegrown rabbit winner in case). question how stop other threads when conditions met? tried interrupted() method couldn't see differences.
homegrown rabbit @ position 5 homegrown rabbit @ position 10 wild turtle @ position 3 wild turtle @ position 6 homegrown rabbit @ position 15 homegrown rabbit @ position 20 wild turtle @ position 9 wild turtle @ position 12 wild turtle @ position 15 homegrown rabbit @ position 25 wild turtle @ position 18 homegrown rabbit @ position 30 wild turtle @ position 21 homegrown rabbit @ position 35 wild turtle @ position 24 homegrown rabbit @ position 40 wild turtle @ position 27 wild turtle @ position 30 homegrown rabbit @ position 45 homegrown rabbit @ position 50 wild turtle @ position 33 homegrown rabbit @ position 55 wild turtle @ position 36 wild turtle @ position 39 homegrown rabbit @ position 60 wild turtle @ position 42 wild turtle @ position 45 homegrown rabbit @ position 65 wild turtle @ position 48 wild turtle @ position 51 homegrown rabbit @ position 70 homegrown rabbit @ position 75 wild turtle @ position 54 wild turtle @ position 57 wild turtle @ position 60 homegrown rabbit @ position 80 homegrown rabbit @ position 85 wild turtle @ position 63 wild turtle @ position 66 homegrown rabbit @ position 90 wild turtle @ position 69 wild turtle @ position 72 wild turtle @ position 75 homegrown rabbit @ position 95 wild turtle @ position 78 homegrown rabbit @ position 100 homegrown rabbit winner of race! wild turtle @ position 81 wild turtle @ position 84 wild turtle @ position 87 wild turtle @ position 90 wild turtle @ position 93 wild turtle @ position 96 wild turtle @ position 99 wild turtle @ position 102 wild turtle winner of race!
you don't need thread primitives stop thread. need exit while loop.
here modified run
public void run() { while (winner == false) { position += speed; system.out.println(name + " @ position " + position ); if(position >= 100) { winner = true; system.out.println(name + " winner of race!"); break; } try { thread.sleep((long) (0 + (math.random() * restmax))); } catch (interruptedexception e) { e.printstacktrace(); } } }
Comments
Post a Comment