Presentation is loading. Please wait.

Presentation is loading. Please wait.

12/1/98 COP 4020 Programming Languages Parallel Programming in Ada and Java Gregory A. Riccardi Department of Computer Science Florida State University.

Similar presentations


Presentation on theme: "12/1/98 COP 4020 Programming Languages Parallel Programming in Ada and Java Gregory A. Riccardi Department of Computer Science Florida State University."— Presentation transcript:

1 12/1/98 COP 4020 Programming Languages Parallel Programming in Ada and Java Gregory A. Riccardi Department of Computer Science Florida State University December 1, 1998

2 12/1/98 Two Strategies for Parallelism n Ada, task synchronization n Tasks execute independently n Rendezvous provides synchronized execution between 2 tasks n Java, method and object synchronization n Threads execute independently n Synchronized methods are executed as critical sections n only one execution per synch. method n Synchronized statements depend on objects n one execution per object of any statement

3 12/1/98 Tasks in Ada n Ada supports a program unit declaration of tasks n Each task object represents a separate unit of execution n Tasks can share objects through usual global packages and nesting n Task T is... end; -- declaration of a single task object n Task type TT is... end; -- type for many objects n T1: TT; T2: TT; -- T1 and T2 are task objects n Example using global variables g and h n task type TT ; n task body TT is x: integer; begin x := g; h := x; end; n -- each task of type TT uses global g n T1, T2: TT; g:= 4; g := 5; g:=6; n what is value of h? n this is a race condition. n type ptrT is access T;

4 12/1/98 Synchronization and Communication n Synchronization is required to eliminate race conditions. n Ada tasks synchronize by 'rendezvous' n Two tasks, one is caller, one is receiver n Caller requests a meeting for a specific purpose n Receiver offers to accept a meeting for any of a specific collection of purposes n Meeting takes place: n Caller passes information to receiver and waits n Receiver gets information, executes by itself, passes information back and continues its execution n Caller gets information from receiver and continues n Place (or purpose) is called entry like procedure specification n Call is an entry call, syntax exactly like procedure call n Receive is an accept, very much like a procedure body

5 Task Execution and Termination n declare -- beginning of block n T1: TT; -- T1 gets ready to execute, stack created n begin -- T1 begins its execution here n -- T1 executes in parallel with this code n end; -- block is not completely finished until T1 finishes. n declare -- beginning of block n Tptr: TT * = new TT; -- *Tptr gets ready to execute, and begins n begin n -- T1 executes in parallel with this code n end; -- block does not wait for *Tptr to finish. n Terminate T1; -- immediately terminates execution of T1

6 12/1/98 Storage Management for Tasks n Does each task need its own stack? n Each task has access to other stacks appropriate to its nesting n Guarantees of mutual exclusion n Two tasks that wish to access shared objects n Access only during rendezvous, receiver accesses object and passes value to caller n Use rendezvous to synchronize n Caller references variable, then makes entry call n Receiver waits to reference until it has received a call n The accept statement is guaranteed to execute while the caller is blocked. n Create a third task to provide access to shared data n New task is a monitor. n Create a message passing task to send/receive messages

7 12/1/98 Threads in Java n class MyThread extends Thread { public void run() {// code for execution n MyThread threadVar = new MyThread(); threadVar.start(); n class MyRunner implements Runnable { public void run() { // code for execution n MyRunner runVar = new MyRunner(); new Thread(runVar).start(); n Other methods n stop, sleep, suspend, resume, yield, destroy

8 12/1/98 Synchronization in Java n Synchronization uses locks and monitors n Objects can be locked n Methods can have exclusive execution n synchronized statement n synchronized (expression) statement n obtain lock for object or array then execute stmt n Example of sorting a shared array n synchronized (a) {//sort array here…} n synchronized modifier n method that is executed exclusively n class MyObject { n public synchronized void update()...

9 12/1/98 Distributed Objects in Java n Java supports communication between programs n java.net.Socket and Java.net.ServerSocket n To send objects to another program n Socket sock = new Socket(host,port); ObjectOutputStream out = new ObjectOutputStream (sock.getOutputStream(); out.writeObject(out); // object is transferred! n To receive object, n create a server socket n open ObjectInputStream n readObject


Download ppt "12/1/98 COP 4020 Programming Languages Parallel Programming in Ada and Java Gregory A. Riccardi Department of Computer Science Florida State University."

Similar presentations


Ads by Google