wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads and Synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads and Synchronization" Watch "Threads and Synchronization" New topic
Author

Threads and Synchronization

adil zahir
Greenhorn

Joined: May 09, 2013
Posts: 29


please someone can explain for me the output i'm lost
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1049
    
  15
Ok, well since you forgot to append the output that you couldn't follow, here it is for all.

run:
5
4
3
2
1
BUILD SUCCESSFUL (total time: 1 second)

Also your post needs indentations and formatting.

So which part of the output is not making sense to you? Also what is this for?

Chan.

Luan Cestari
Ranch Hand

Joined: Feb 07, 2010
Posts: 163

I think in some conditions the output could change, but this code if very strange, did you get from the exam? I don't remember thread question with a code like this.


Please, visit me for some cool tech post at www.ourdailycodes.com
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1049
    
  15
I think in some conditions the output could change


In what conditions could the output be different? Is there something I am missing?
Luan Cestari
Ranch Hand

Joined: Feb 07, 2010
Posts: 163

In the "view" method and during the time when x will be decreased to 0, the other thread can just have called adjust and waiting the lock/synchronization (so Thread1 will be entering the "adjust" method and the Thread2 will be calling "adjust" and getting its locking waiting to run it). In this scenario, it would be "-1" printed. What do you think about this scenario (like possible or not and why)? (It is good two heads thinking about the problem to solve it faster =) )
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 812
    
    1


t1 starts and calls h.hold and then synchronize h when the view() is called.
t2 starts concurrently, calls h2.view then synchronize h2 when the adjust() is called.

There should not be any deadlock as t1 only synchronize h and t2 only synchronize h2.

Deadlock will occur if two threads are calling these two methods concurrently :


or

Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1049
    
  15
In the "view" method and during the time when x will be decreased to 0, the other thread can just have called adjust and waiting the lock/synchronization (so Thread1 will be entering the "adjust" method and the Thread2 will be calling "adjust" and getting its locking waiting to run it). In this scenario, it would be "-1" printed. What do you think about this scenario (like possible or not and why)? (It is good two heads thinking about the problem to solve it faster =) )


Thanks for your response on this one, Luan. I will get back on this one later. Thanks.

Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1049
    
  15
Heya, Luan.

Sorry for the delay. In my opinion, yes you're right ( thanks for making me see that case ). The thing you mentioned could be a case too, just the number to be printed would be 0, not -1. But then again, both the threads are accessing the same static variable simultaneously through different objects, so even a completely different output could also result ( like for instance x could be decremented twice before a print ). I could actually create the case of when the first thread runs after the second thread and prints 0 by adding a delay in the run method. But this could still happen ( though unlikely ) without the delay also.

Here's a slightly formatted version of the original code.



I've added a small delay in the run method. Just that is the change. With this code, I got the following output.
run:
5
4
3
2
1
0
BUILD SUCCESSFUL (total time: 6 seconds)

But I think that it's possible ( although unlikely ) that we would get the same scheduling scheme without creating our own delay and also that we could get a different output ( I think so - my I think so statements are statements with disclaimers ). What do you think?

Thanks,
Chan.

Edit : To the OP- I believe if you're posting this from some reference book or so, it's good to quote your sources. Ignore if it is your own code.
Luan Cestari
Ranch Hand

Joined: Feb 07, 2010
Posts: 163

Hi Chan! =D

Nice code! You really put effort on that, pretty cool =D Sorry for I said wrong "-1" btu you get the idea. Yeah that would be very unlike, but if we put some real world scenario where the business code and some more integration could obfuscate and make that delay happen more often IMO =)

I like those concurrent problems (not in production, of course lol) due you need to think very carefully and in a different way that non-concurrent problem are. I would also give an advice that we could create an test case using Byteman (from JBoss) which would intercept any point of the code we want and insert some code (AOP). This way we could replicate some strange scenarios like this one =D

Best regards!

Luan
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1049
    
  15

I would also give an advice that we could create an test case using Byteman (from JBoss) which would intercept any point of the code we want and insert some code (AOP). This way we could replicate some strange scenarios like this one =D


Thanks, Luan. I'll try it sometime later ( after I've worked on a couple of prerequisites to be able to implement the above ).
 
wood burning stoves
 
subject: Threads and Synchronization