Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package runners_advanced;
- public class Runner implements Runnable
- {
- private int runnerID;
- private Start start;
- public static final int COUNTER = 100;
- public Runner(int id, Start s)
- {
- runnerID = id;
- start = s;
- }
- public int getRunnerID()
- {
- return runnerID;
- }
- public String toString()
- {
- return "Runner " + runnerID;
- }
- public void run()
- {
- int metresRun = 0;
- while(metresRun < COUNTER)
- {
- boolean check = start.ready(); // Checks if the current runner is the last runner
- start.setReady(this);
- try
- {
- if(start.ready())
- {
- if(start.ready() != check)
- Thread.sleep((long)(Math.random() * 10)); // Makes the last runner sleep so that they don't start before the others all the time
- metresRun++;
- Thread.sleep((long)(Math.random() * 10));
- System.out.println(toString() + " has run " + metresRun + " metres");
- }
- else System.out.println(toString() + " waiting...");
- }
- catch(InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- System.out.println(toString() + " has arrived!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement