Biblioteca Java - Blame information for rev 27
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
27 | mihai | 1 | package ex3_roadtraffic; |
2 | /* | ||
3 | A bridge over a river contains only a single lane, but cars enter from both direction. | ||
4 | Consequently, some kind of synchronisation is needed to keep cars from colliding. | ||
5 | To illustrate the problem we provide you with a malfunctioning solution, in which no synchronisation is performed. | ||
6 | Your task is to add synchronisation to the cars. | ||
7 | |||
8 | All you need to know is that cars going from left to right call the method controller.enterLeft() when they | ||
9 | approach the bridge (to ask for permission) and call the method controller.leaveRight() when they leave. | ||
10 | Cars in the other direction call enterRight and leaveLeft instead. Here, controller is an instance of the | ||
11 | class TrafficController, which is responsible for synchronisation. As you can see, the supplied class has | ||
12 | empty implementations of all four methods. Your task is to change this class into a monitor that coordinates | ||
13 | cars so that they do not collide. | ||
14 | |||
15 | */ | ||
16 | public class Main { | ||
17 | |||
18 | private static void nap(int ms) { | ||
19 | try { | ||
20 | Thread.sleep(ms); | ||
21 | } catch (InterruptedException e) {} | ||
22 | } | ||
23 | |||
24 | |||
25 | public static void main(String[] a) { | ||
26 | final CarWindow win = new CarWindow(); | ||
27 | |||
28 | win.pack(); | ||
29 | win.setVisible(true); | ||
30 | |||
31 | new Thread(new Runnable() { | ||
32 | public void run() { | ||
33 | while (true) { | ||
34 | nap(25); | ||
35 | win.repaint(); | ||
36 | } | ||
37 | } | ||
38 | }).start(); | ||
39 | |||
40 | |||
41 | |||
42 | } | ||
43 | |||
44 | } |