Biblioteca Java - Rev 27

Subversion Repositories:
Rev:
package ex1_airport;
import java.util.concurrent.*;

/*
In this example we will consider a display of the kind commonly found in airports,
where information about upcoming flights is given on a electronic display board.

The hardware is accessed using the following simple API, found in the file HWDisplay.java

It is clear that the interface HWDisplay is much too low-level for actually programming the display. Therefore,
the manufacturer provides also HighLevelDisplay.java, which is much more convenient to use.

JDisplay and JDisplay2 classes are provided in order to access and use display hardware.

TASK 1
We will not have time to try to think of a full-fledged program for the airport
administration using this API, but just note that it is conceivable that the display
will be accessed from a multi-threaded program, with several airport officials
concurrently updating the display. To test the class JDisplay2 under such conditions,
your task now is to write a simple multi-threaded program. A skeleton is found in
the file Main3.java, which shows the structure of a simple main program that creates
a display d and starts two threads, one executing the static procedure addProc(d),
the other executing deleteProc(d). You must complete the bodies of these two procedures.
Fill addProc with a sequence of addRow commands, interspersed with suitable naps.
Similarly, fill deleteProc with calls to deleteRow(0).

If you do this and run your program, you will probably see some un-intended behaviour.
You should make sure that you understand how these problems can occur. In fact, the
class JDisplay2 is not thread safe; it does not guarantee correct behaviour when its
methods are accessed by concurrent threads.
 
TASK 1
The airport IT department, developing the application Main3, does not have
access to JDisplay2, but only to the interface and an object file. Thus they
have to solve the problem without modifying JDisplay2. One way to do this is
to identify the critical sections in Main3 and protect them using a semaphore.
Do this! You should use an instance of java.util.concurrent.Semaphore for this
purpose. (java.util.concurrent is a new package from Java 1.5.) Note that the method acquire()
may throw an InterruptedException, so you will have to use a try/catch construction.
Test your program again to see that it seems to behave correctly. We say "seems", since
you should now have an initial feeling for the difficulties in testing a concurrent program.

TASK 2
The IT department has also complained to the display manufacturer, requesting a thread safe
implementation of JDisplay2. Fulfill this request.
*/


public class Main3 {

   private static void sleep(int millisecs) {
        try {
            Thread.sleep(millisecs);
        } catch (InterruptedException e) {
            System.err.println(e.getMessage());
        }
    }
   
    private static void addProc(HighLevelDisplay d) {

        // Add a sequence of addRow operations with short random naps.
    for(int i=0;i<20;i++){
        d.addRow("Info traffic "+i);
        sleep(1000);
    }
   
   }

    private static void deleteProc(HighLevelDisplay d) {
       
        // Add a sequence of deletions of row 0 with short random naps.
        for(int i=0;i<20;i++){
                d.deleteRow(0);
                sleep(3000);
        }
   
       
       
    }

    public static void main(String [] args) {
       
        final HighLevelDisplay d = new JDisplay2();
       
       
       
    }
}