Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads and Synchronization

 
adil zahir
Greenhorn
Posts: 29
Chrome Java ME Mac
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

please someone can explain for me the output i'm lost
 
Chan Ag
Rancher
Posts: 1089
14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
C++ Redhat Ruby
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chan Ag
Rancher
Posts: 1089
14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
C++ Redhat Ruby
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1142
4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Rancher
Posts: 1089
14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1089
14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
C++ Redhat Ruby
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1089
14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ).
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic