.


:




:

































 

 

 

 


i ii




i i i .

class SendingThread extends Thread { Semaphore semaphore = null; SendingThread(Semaphore semaphore) { this.semaphore = semaphore; System.out.println("SendingThread created"); } public void run() { System.out.println("SendingThread started"); for (int i = 0; i < 6; ++i) { System.out.println("SendingThread do(" + i + ")"); this.semaphore.take(i); } System.out.println("SendingThread stoped"); } } class ReceivingThread extends Thread { Semaphore semaphore = null; ReceivingThread(Semaphore semaphore) { this.semaphore = semaphore; System.out.println("ReceivingThread created"); } public void run() { System.out.println("ReceivingThread started"); while(true) { try { int m = this.semaphore.release(); System.out.println("ReceivingThread do(" + m + ")"); } catch (InterruptedException ie) { } } } } class Semaphore { private boolean signal = false; private int message = 0; public synchronized void take(int m) { this.signal = true; this.notify(); message = m; System.out.println("Semaphore take"); } public synchronized int release() throws InterruptedException{ while(!this.signal) { wait(); } this.signal = false; System.out.println("Semaphore release"); return(message); } } public class SignalingSemaphore { public static void main(String argc[]) { System.out.println("Main process started"); Semaphore semaphore = new Semaphore(); SendingThread sender = new SendingThread(semaphore); ReceivingThread receiver = new ReceivingThread(semaphore); receiver.start(); sender.start(); System.out.println("Main process ended"); } }

i sender i receiver semaphore i i .

i sender i ' i take() receiver.

receiver i i release().

i receiver i i .

, i i i ii i CTRL+C.

 

Ii ii i ii i / i.

i i .

i i.

class SendingThread extends Thread { CountingSemaphore semaphore = null; SendingThread(CountingSemaphore semaphore) { this.semaphore = semaphore; System.out.println("SendingThread created"); } public void run() { System.out.println("SendingThread started"); for (int i = 0; i < 6; ++i) { System.out.println("SendingThread do(" + i + ")"); this.semaphore.take(i); } System.out.println("SendingThread stoped"); } } class ReceivingThread extends Thread { CountingSemaphore semaphore = null; ReceivingThread(CountingSemaphore semaphore) { this.semaphore = semaphore; System.out.println("ReceivingThread created"); } public void run() { int sum = 0; System.out.println("ReceivingThread started"); while(true) { try { int m = this.semaphore.release(); sum += m; System.out.println("ReceivingThread do(" + m + "): " + sum); } catch (InterruptedException ie) { } } } } class CountingSemaphore { private int signals = 0; private int message = 0; public synchronized void take(int m) { this.signals++; message = m; this.notify(); System.out.println("CountingSemaphore(take): " + this.signals); } public synchronized int release() throws InterruptedException { while(this.signals == 0) { wait(); } this.signals--; System.out.println("CountingSemaphore(release): " + this.signals); return(message); } } public class CountingSemaphoreDemo { public static void main(String argc[]) { System.out.println("Main process started"); CountingSemaphore semaphore = new CountingSemaphore(); SendingThread sender1 = new SendingThread(semaphore); SendingThread sender2 = new SendingThread(semaphore); ReceivingThread receiver = new ReceivingThread(semaphore); receiver.start(); sender1.start(); sender2.start(); System.out.println("Main process ended"); } }

ii ii i, i .

 

i ii ii i.

i i ii i, i i i .

ii i ii ii i , i i .

i .

class SendingThread extends Thread { BoundedSemaphore semaphore = null; String name; long procNum; SendingThread(String n, long p, BoundedSemaphore semaphore) { this.semaphore = semaphore; name = n; procNum = p; System.out.println(name + ": created"); } public void run() { System.out.println(name + ": started"); for (int i = 0; i < 4; ++i) { System.out.println(name + ": do(" + (i * procNum) + ")"); try { this.semaphore.take(i * procNum); } catch (InterruptedException ie) { } } System.out.println(name + ": stoped"); } } class ReceivingThread extends Thread { BoundedSemaphore semaphore = null; String name; int procNum; long sum = 0; ReceivingThread(String n, int p, BoundedSemaphore semaphore) { this.semaphore = semaphore; name = n; procNum = p; System.out.println(name + ": created"); } public void run() { System.out.println(name + ": started"); while(true) { try { long m = this.semaphore.release(); sum += m; System.out.println(name + ": do(" + m + "), " + sum); } catch (InterruptedException ie) { } } } } class BoundedSemaphore { private int signals = 0; private int bound = 0; private long message = 0; public BoundedSemaphore(int upperBound) { this.bound = upperBound; } public synchronized void take(long m) throws InterruptedException { while(this.signals == bound) { wait(); } this.signals++; System.out.println("BoundedSemaphore(take): " + this.signals); message = m; this.notify(); } public synchronized long release() throws InterruptedException { while(this.signals == 0) { wait(); } this.signals--; System.out.println("BoundedSemaphore(release): " + this.signals); long m = message; message = 0; this.notify(); return(m); } } public class BoundedSemaphoreDemo { public static void main(String argc[]) { System.out.println("Main process started"); BoundedSemaphore semaphore = new BoundedSemaphore(3); SendingThread sender1 = new SendingThread("Sender 1", 1, semaphore); SendingThread sender2 = new SendingThread("Sender 2", 10, semaphore); SendingThread sender3 = new SendingThread("Sender 3", 100, semaphore); SendingThread sender4 = new SendingThread("Sender 4", 1000, semaphore); SendingThread sender5 = new SendingThread("Sender 5", 10000, semaphore); SendingThread sender6 = new SendingThread("Sender 6", 100000, semaphore); ReceivingThread receiver = new ReceivingThread("Receiver", 1, semaphore); receiver.start(); sender1.start(); sender2.start(); sender3.start(); sender4.start(); sender5.start(); sender6.start(); System.out.println("Main process ended"); } }

i i i i signals, i bound.

ii i, i i, .

 

  1. , ii i .
  2. ' ii i.
  3. ' i , i.

 


 

7.





:


: 2016-11-24; !; : 452 |


:

:

- , - .
==> ...

1508 - | 1427 -


© 2015-2024 lektsii.org - -

: 0.013 .