• Post Reply Bookmark Topic Watch Topic
  • New Topic

Semaphore  RSS feed

 
Santosh Kumar Nayak
Ranch Hand
Posts: 193
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Can some one help me to understand the output ?
Every time I execute the code the output is different 


Below is my output :-

Starting A
Starting B
A is waiting for a permit.
B is waiting for a permit.
A gets a permit.
A: 1
A: 2
A: 3
A: 4
A: 5
A releases the permit.
B gets a permit.
B: 4
B: 3
B: 2
B: 1
B: 0
B releases the permit.
count: 0

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every time I execute the code the output is different

In general the OS determines what thread to run next.  What criteria it uses to select a thread can depend on what else is happening in the computer. 

Can you post some examples of the different output so we can see what you are talking about?
 
Santosh Kumar Nayak
Ranch Hand
Posts: 193
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another Output :

Starting A
A is waiting for a permit.
Starting B
A gets a permit.
B is waiting for a permit.
A: 1
A: 2
A: 3
A: 4
A: 5
A releases the permit.
B gets a permit.
B: 4
B: 3
B: 2
B: 1
B: 0
B releases the permit.
count: 0

There should be some mechanism by which we can control the thread execution sequence
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another Output : 

Can you show what is different about the outputs?

some mechanism by which we can control the thread execution sequence 

Yes, there are.  I don't have any examples handy, but I'm sure someone here on the forum will come and show you some.

A simple way is to not use threads.  Call the methods in the order that you want them to execute.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Santosh Kumar Nayak wrote:There should be some mechanism by which we can control the thread execution sequence

Yes, you do that with classes such as Semaphore, Lock and Condition, which you did. That's why all the lines starting with "A:" and all the lines starting with "B:" are not being interleaved. If you don't want B to start until after A started waiting for a permit, you need to synchronize that as well.

As you can see, that's pretty overkill for synchronizing one println() statement. Why would you even worry about whether A or B prints that they have started first?
 
Santosh Kumar Nayak
Ranch Hand
Posts: 193
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stephan

I got the answer for my query.Basically which ever thread A or B get the permit first to execute it starts executing and makes changes to shared variable.
After a thread invokes release then control passes to another thread.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!