Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban...

16
Instructore: Tasneem Darwish 1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and Real Time programming Implementing communication paradigms in java

Transcript of Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban...

Page 1: Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.

Instructore: Tasneem Darwish 1

University of PalestineFaculty of Applied Engineering and Urban Planning

Software Engineering Department

Concurrent and Real Time programming

Implementing communication paradigms in java

Page 2: Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.

Instructore: Tasneem Darwish 2

Outlines Introduction

Semaphores

Signals

Events

Buffers

Blackboards

Broadcasts

Barriers.

Page 3: Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.

Instructore: Tasneem Darwish 3

Signals Often a thread needs to wait for a signal from another thread before it can proceed. There are various types of signals.

A persistent signal is a signal that remains set until a single thread has received it. A transient signal (or pulse) is a signal that releases one or more waiting threads but is lost if no threads are waiting.

Page 4: Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.

Instructore: Tasneem Darwish 4

Signals package communicationAbstractions;

public interface SignalSender {

void send();}

package communicationAbstractions;

public interface SignalWaiter {

void waits() throws InterruptedException;}

Page 5: Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.

Instructore: Tasneem Darwish 5

Signals package communicationAbstractions;

public abstract class Signal implements SignalSender, SignalWaiter {

public synchronized void send() {

arrived = true;notify();

}

public abstract void waitS() throws InterruptedException;

protected boolean arrived = false;}

the implementation of the send operation has code common for all signals

Page 6: Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.

Instructore: Tasneem Darwish 6

Persistent signalspackage communicationAbstractions;

public interface SignalWaiterOrWatcher extends SignalWaiter {

boolean watch();}

package communicationAbstractions;public class PersistentSignal extends Signal implements SignalWaiterOrWatcher{

public synchronized void waitS() throws InterruptedException {

while(!arrived) wait(); // Wait for a new signal.arrived = false;

}public synchronized boolean watch() {

// This method never waits.if(!arrived) return false;arrived = false;return true;

}}

Page 7: Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.

Instructore: Tasneem Darwish 7

Persistent Signals

Unlike semaphores, send method call will release only one thread no count is maintained.

Page 8: Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.

Instructore: Tasneem Darwish 8

Transient Signals A transient signal is a signal that is lost if no threads are currently waiting. Two types can be recognized:

one that releases a single thread, and one that releases all threads (called a pulse).

first the simple transient signal that releases just one thread. This extends the abstract Signal class (that implements the SignalSender and SignalWaiter interfaces)

Page 9: Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.

Instructore: Tasneem Darwish 9

Transient signalspackage communicationAbstractions;public class TransientSignal extends Signal {

public synchronized void send() {

// Overrides send in Signal// and implements the SignalSender interface.if(waiting > 0) super.send(); // it will not send a signal unless there

is a waiting thread}public synchronized void waitS() throws InterruptedException{

// Overrides waitS in Signal and// implements the SignalWaiter interface.try { while(!arrived)

{waiting++;wait();waiting--;

}arrived = false;

} catch(InterruptedException ie) { waiting--; throw ie;}}protected int waiting = 0; }

Page 10: Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.

Instructore: Tasneem Darwish 10

Transient signals

If the catch doesn’t decrement the wait variable, the result would be that eventually a transient signal would be made permanent.

Page 11: Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.

Instructore: Tasneem Darwish 11

PulsesA Pulse allows all waiting threads to be released.

package communicationAbstractions;

public class Pulse extends TransientSignal {

public synchronized void sendAll() { // A new method.

if(waiting > 0) {

arrived = true;notifyAll();

}}

Page 12: Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.

Instructore: Tasneem Darwish 12

Pulsespublic synchronized void waitS() throws InterruptedException {

// Overrides waitS in TransientSignal and// implements the SignalWaiter interface.try {

while(!arrived) {

waiting++;wait();waiting--;

}if(waiting == 0) arrived = false;

} catch(InterruptedException ie) {

if(--waiting == 0) arrived = false;throw ie;

}}

}

Page 13: Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.

Instructore: Tasneem Darwish 13

PulsesIf more than one thread is waiting for the signal and

sendAll is called, all are released. The last one detects that there are no more threads to be

released and sets the boolean flag to false. Any threads that queue after this point will have to wait

for the next signal.

Page 14: Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.

Instructore: Tasneem Darwish 14

Events

An event is a bivalued variable (UP or DOWN).

A thread can wait for an event to be set or reset.

Events may be created and initialized using the class Event.

The method set causes the event to go into the UP state; the

method reset causes it to go into the DOWN state.

The toggle procedure simply changes the state of the event

from UP to DOWN, or from DOWN to UP.

The function state returns the current state of the event.

Synchronization with an event is achieved using the await

method.

Page 15: Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.

Instructore: Tasneem Darwish 15

Eventspackage communicationAbstractions;public enum EventState {UP, DOWN};package communicationAbstractions;public class Event {

public Event(EventState initial) {

value = initial;}

public Event() { value = EventState.DOWN; }

public synchronized void await(int state)throws InterruptedException {

while(value != state) wait();}

public synchronized void set() {

value = EventState.UP;notifyAll();

}

Page 16: Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.

Instructore: Tasneem Darwish 16

Eventspublic synchronized void reset() {

value = EventState.DOWN;notifyAll();

}public synchronized void toggle() {

if(value == EventState.DOWN) value = EventState.UP;else value = EventState.DOWN;notifyAll();

}