• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Synchronization in Threads

 
MohamedSalim malik
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey friends , i have this problem with Thread Synchronization. please look at the following class where i created two threads by implementing Runnable interface. in here i can only get the results ABAB, Because
I am using two different threads with two different runnable targets. Am i correct. I only want to know my theory is correct? please somebody tell me.





and The second class , I can get two results as "ABAB" , and "AABB" . am i right? It is because i am using the same runnable target for different Threads . Is my theory correct. or please somebody tell me if the way i understand is wrong. and please forgive my bad use of English.






 
Sebanti Sanyal
Ranch Hand
Posts: 58
C++ Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is just the opposite. First example uses two different Runnable targets for the two threads. These two threads can concurrently execute the run() method. Hence, the output can be AABB or ABAB.
The second example uses the same Runnable object while creating two threads. The 'synchronized' keyword actually comes into play here.Once a thread is inside the run() method, the other thread has to wait for the former to complete. Therefore, the output has to be ABAB.
 
MohamedSalim malik
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, I got it now. Thanks a lot Sebanti Sanyal for your Short and Sweet answer. You are awesome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic