Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
titleTaxi-threads
linenumberstrue
public class Taxi extends Thread {
	
	public static int instances;
	
	private int taxinr;  
	
	public Taxi() {
		taxinr = Taxi.instances++;
	}
	
	public void run() {
		
		int initialwait = (int) (Math.random()*1000);
		int tempwait;
		
		try {
			Thread.sleep(initialwait);
		} catch (InterruptedException e1) {
			// nix
		}
	
		for (int i = 1; i < (int) (Math.random()*5) + 1 ; i++) {
			
			System.out.println("Taxi " + taxinr + " has drived " + i + " km");
		
			tempwait = (int) (Math.random()*1000);
			
			try {
				Thread.sleep(tempwait);
			} catch (InterruptedException e) {
				// nix 
			}
		}
	}
	
	public static void main(String[] args) {
		
		for (int i = 0; i < 5; i++) {
			(new Taxi()).start();			
		}
	}
}