Biblioteca Java - Blame information for rev 27
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
27 | mihai | 1 | package ex1_airport; |
2 | import java.util.concurrent.*; | ||
3 | |||
4 | /* | ||
5 | In this example we will consider a display of the kind commonly found in airports, | ||
6 | where information about upcoming flights is given on a electronic display board. | ||
7 | |||
8 | The hardware is accessed using the following simple API, found in the file HWDisplay.java | ||
9 | |||
10 | It is clear that the interface HWDisplay is much too low-level for actually programming the display. Therefore, | ||
11 | the manufacturer provides also HighLevelDisplay.java, which is much more convenient to use. | ||
12 | |||
13 | JDisplay and JDisplay2 classes are provided in order to access and use display hardware. | ||
14 | |||
15 | TASK 1 | ||
16 | We will not have time to try to think of a full-fledged program for the airport | ||
17 | administration using this API, but just note that it is conceivable that the display | ||
18 | will be accessed from a multi-threaded program, with several airport officials | ||
19 | concurrently updating the display. To test the class JDisplay2 under such conditions, | ||
20 | your task now is to write a simple multi-threaded program. A skeleton is found in | ||
21 | the file Main3.java, which shows the structure of a simple main program that creates | ||
22 | a display d and starts two threads, one executing the static procedure addProc(d), | ||
23 | the other executing deleteProc(d). You must complete the bodies of these two procedures. | ||
24 | Fill addProc with a sequence of addRow commands, interspersed with suitable naps. | ||
25 | Similarly, fill deleteProc with calls to deleteRow(0). | ||
26 | |||
27 | If you do this and run your program, you will probably see some un-intended behaviour. | ||
28 | You should make sure that you understand how these problems can occur. In fact, the | ||
29 | class JDisplay2 is not thread safe; it does not guarantee correct behaviour when its | ||
30 | methods are accessed by concurrent threads. | ||
31 | |||
32 | TASK 1 | ||
33 | The airport IT department, developing the application Main3, does not have | ||
34 | access to JDisplay2, but only to the interface and an object file. Thus they | ||
35 | have to solve the problem without modifying JDisplay2. One way to do this is | ||
36 | to identify the critical sections in Main3 and protect them using a semaphore. | ||
37 | Do this! You should use an instance of java.util.concurrent.Semaphore for this | ||
38 | purpose. (java.util.concurrent is a new package from Java 1.5.) Note that the method acquire() | ||
39 | may throw an InterruptedException, so you will have to use a try/catch construction. | ||
40 | Test your program again to see that it seems to behave correctly. We say "seems", since | ||
41 | you should now have an initial feeling for the difficulties in testing a concurrent program. | ||
42 | |||
43 | TASK 2 | ||
44 | The IT department has also complained to the display manufacturer, requesting a thread safe | ||
45 | implementation of JDisplay2. Fulfill this request. | ||
46 | */ | ||
47 | |||
48 | public class Main3 { | ||
49 | |||
50 | private static void sleep(int millisecs) { | ||
51 | try { | ||
52 | Thread.sleep(millisecs); | ||
53 | } catch (InterruptedException e) { | ||
54 | System.err.println(e.getMessage()); | ||
55 | } | ||
56 | } | ||
57 | |||
58 | private static void addProc(HighLevelDisplay d) { | ||
59 | |||
60 | // Add a sequence of addRow operations with short random naps. | ||
61 | for(int i=0;i<20;i++){ | ||
62 | d.addRow("Info traffic "+i); | ||
63 | sleep(1000); | ||
64 | } | ||
65 | |||
66 | } | ||
67 | |||
68 | private static void deleteProc(HighLevelDisplay d) { | ||
69 | |||
70 | // Add a sequence of deletions of row 0 with short random naps. | ||
71 | for(int i=0;i<20;i++){ | ||
72 | d.deleteRow(0); | ||
73 | sleep(3000); | ||
74 | } | ||
75 | |||
76 | |||
77 | |||
78 | } | ||
79 | |||
80 | public static void main(String [] args) { | ||
81 | |||
82 | final HighLevelDisplay d = new JDisplay2(); | ||
83 | |||
84 | |||
85 | |||
86 | } | ||
87 | } |