|
public class road { public static void main(string []args) { car c=new car(); driver d =new driver(c); passenger p =new passenger(c); new thread(d).start(); new thread(p).start();
} } class car { synchronized public void sleep() { try{ wait(); } catch(exception e) { } } synchronized public void week() { notify(); } }
class passenger implements runnable { car c; public passenger() { } public passenger(car c) { this.c=c; } public void run() { while(true) { thread.yield(); try{ thread.sleep(3000); }catch(interruptedexception ie) { } system.out.println("a passenger go on"); c.week(); system.out.println("waiting stop and sleep..."); c.sleep();
} }
}
class driver implements runnable { private car c; public driver() { } public driver(car c) { this.c=c; } public void run() { while(true) { system.out.println("wait a passenger......."); c.sleep(); try{ thread.sleep(3000); }catch(interruptedexception ie) { } thread.yield(); try{ thread.sleep(100); }catch(interruptedexception ie) { } c.week(); system.out.println(" passenger week and go ");
} } }
|