java - How to use Semaphores for Signaling? -
Now I check semaphores I went to the following link about this topic:
< / P>
The author of this link has written about using SACFASS for signaling. To show how he works, he wrote a custom semaphore.
Custom semaphore code:
public class semaphore {private boolean sign = false; Public synchronized zero () {this.signal = true; This.notify (); } Public synchronized zero releases () throws interrupted exceptions {while (! This.signal) wait (); This. Signal = false; }} How it's used in code:
public class sendingThread {Semaphore Semifier = null; Public transmittance (semaphore semaphore) {this.semaphore = semaphore; } Run public null () {while (true) {// something to do, then it signals semaphore.take (); }}} Public Class Receiving Thread {Semaphore Semifar = Faucet; Public recepted (semaphore semaphore) {this.semaphore = semaphore; } Public run zero () {while (true) {this.semaphore.release (); // Get the signal, then do something ...}}} main:
semaphore semaphore = new semaphore (); SentTrade Sender = New SendStread (Sikandra); Receiver Receiver Receiver = Receiving New (Sikandra); Receiver.start (); Sender.start (); As I knew that the order of execution should follow
Send - Send received - get received - get ... get ... I tried to write my code using this blueprint
public class SendReceiveWithCustomSemaphore {public static zero main (string [] args) {MySemaphore mySemaphore = new MySemaphore (); Send new (messiframe) Start (); Get new (mesoforphs). Start (); }} Class MySemaphore {Boolean flag = false; Public synchronized zero () throws blocker encapsulation {flag = true; Notify(); } Public synchronized zero releases () bounce splits interception exception {while (! Flag) {wait (); } flag = false; }} Increase the send class thread {Maisonfair Mimfer; Send Public (Mesophore Semaphore) {this.mySemaphore = semaphore; } @ Override Public run zero () {int i = 0; Whereas (i ++> <10) {System.out.println ("send"); {MySemaphore.take (); } Grip (Interrupted e) e.printStackTrace (); }}}} Enhances class receipt thread {Maisonfair Mimfer; Public Receipt (MySmoda Semaphore) {this.mySemaphore = semaphore; } @ Override Public Zero () {While (true) {try {mySemaphore.release ()); } Grip (Interrupted e) e.printStackTrace (); } System.out.println ("Received"); }}} Output:
Send Send Send Send Send Send Send Send Send Send Send Send
I made a mistake, then I wrote the code or did not understand the concept?
What do the writers want to say?
Find a better tutorial.
You can see the output that I expect. The "sender" thread is never blocked, so this "send", "send", "send" will always go on printing. In the meantime, in the "Receiver" thread, every time when he calls the seqfound .release () method, the sender will be stopped until next time.
I hope to see many "send" messages, sometimes mixed in "received" messages - more or less which you describe.
I do not know what to prove this example, but for me, it makes the assumption that the author does not know how the programmers expect the behavior of the saphors.
Some authors have given examples that not to do this, or examples in which there is a deliberate mistake. "In the latter example, are you certain that you Not following an example? Edit: I followed the link, and it seems that the main problem is that the name movements swap and continue () the way if you just change the name, then this more Makes sense.
Comments
Post a Comment