Download presentation
Presentation is loading. Please wait.
Published byAngelica Foster Modified over 9 years ago
1
Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and Real Time programming Implementing communication paradigms in java
2
Instructore: Tasneem Darwish2 Outlines Introduction Semaphores Signals Events Buffers Blackboards Broadcasts Barriers.
3
Instructore: Tasneem Darwish3 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.
4
Instructore: Tasneem Darwish4 Signals package communicationAbstractions; public interface SignalSender { void send(); } package communicationAbstractions; public interface SignalWaiter { void waits() throws InterruptedException; }
5
Instructore: Tasneem Darwish5 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
6
Instructore: Tasneem Darwish6 Persistent signals package 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; }
7
Instructore: Tasneem Darwish7 Persistent Signals Unlike semaphores, send method call will release only one thread no count is maintained.
8
Instructore: Tasneem Darwish8 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)
9
Instructore: Tasneem Darwish9 Transient signals package 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; }
10
Instructore: Tasneem Darwish10 Transient signals If the catch doesn’t decrement the wait variable, the result would be that eventually a transient signal would be made permanent.
11
Instructore: Tasneem Darwish11 Pulses A 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(); }
12
Instructore: Tasneem Darwish12 Pulses public 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; }
13
Instructore: Tasneem Darwish13 Pulses If 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.
14
Instructore: Tasneem Darwish14 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.
15
Instructore: Tasneem Darwish15 Events package 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(); }
16
Instructore: Tasneem Darwish16 Events public synchronized void reset() { value = EventState.DOWN; notifyAll(); } public synchronized void toggle() { if(value == EventState.DOWN) value = EventState.UP; else value = EventState.DOWN; notifyAll(); }
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.