synchronized - Welcome to Computer Science

Download Report

Transcript synchronized - Welcome to Computer Science

COP 4020
Programming Languages
Parallel Programming in Ada and Java
Gregory A. Riccardi
Department of Computer Science
Florida State University
December 1, 1998
12/1/98
Two Strategies for Parallelism


Ada, task synchronization
 Tasks execute independently
 Rendezvous provides synchronized execution
between 2 tasks
Java, method and object synchronization
 Threads execute independently
 Synchronized methods are executed as critical
sections


Synchronized statements depend on objects

12/1/98
only one execution per synch. method
one execution per object of any statement
Tasks in Ada




Ada supports a program unit declaration of tasks
 Each task object represents a separate unit of execution
 Tasks can share objects through usual global packages and
nesting
Task T is . . . end; -- declaration of a single task object
Task type TT is . . . end; -- type for many objects
 T1: TT; T2: TT; -- T1 and T2 are task objects
Example using global variables g and h
 task type TT ;
 task body TT is x: integer; begin x := g; h := x; end;


T1, T2: TT; g:= 4; g := 5; g:=6;



12/1/98
-- each task of type TT uses global g
what is value of h?
this is a race condition.
type ptrT is access T;
Synchronization and Communication


Synchronization is required to eliminate race conditions.
Ada tasks synchronize by 'rendezvous'
 Two tasks , one is caller, one is receiver



Caller requests a meeting for a specific purpose
Receiver offers to accept a meeting for any of a specific
collection of purposes
Meeting takes place:






12/1/98
Caller passes information to receiver and waits
Receiver gets information, executes by itself, passes information
back and continues its execution
Caller gets information from receiver and continues
Place (or purpose) is called entry like procedure
specification
Call is an entry call, syntax exactly like procedure call
Receive is an accept, very much like a procedure body
Task Execution and Termination

declare -- beginning of block
T1: TT; -- T1 gets ready to execute, stack
created
 begin -- T1 begins its execution here
 <statements of block> -- T1 executes in
parallel with this code
 end; -- block is not completely finished until
T1 finishes.
declare -- beginning of block
 Tptr: TT * = new TT; -- *Tptr gets ready to
execute, and begins
 begin


Storage Management for Tasks


Does each task need its own stack?
 Each task has access to other stacks appropriate to its
nesting
Guarantees of mutual exclusion
 Two tasks that wish to access shared objects


Access only during rendezvous, receiver accesses object and
passes value to caller
Use rendezvous to synchronize




12/1/98
The accept statement is guaranteed to execute while the caller
is blocked.
Create a third task to provide access to shared data


Caller references variable, then makes entry call
Receiver waits to reference until it has received a call
New task is a monitor.
Create a message passing task to send/receive messages
Threads in Java



class MyThread extends Thread {
public void run() {// code for execution
 MyThread threadVar = new MyThread();
threadVar.start();
class MyRunner implements Runnable {
public void run() { // code for execution
 MyRunner runVar = new MyRunner();
new Thread(runVar).start();
Other methods
 stop, sleep, suspend, resume, yield,
destroy
12/1/98
Synchronization in Java

Synchronization uses locks and monitors



Objects can be locked
Methods can have exclusive execution
synchronized statement

synchronized (expression) statement

obtain lock for object or array then execute stmt
Example of sorting a shared array



synchronized (a) {//sort array here…}
synchronized modifier
 method that is executed exclusively

class MyObject {

12/1/98
public synchronized void update() ...
Distributed Objects in Java


Java supports communication between programs
 java.net.Socket and Java.net.ServerSocket
To send objects to another program


Socket sock = new Socket(host,port);
ObjectOutputStream out
= new ObjectOutputStream (sock.getOutputStream();
out.writeObject(out); // object is transferred!
To receive object,
 create a server socket
 open ObjectInputStream
 readObject
12/1/98