• Post Reply Bookmark Topic Watch Topic
  • New Topic

Did I Really Find A Bug In Synchronization Or Am I Confused?  RSS feed

 
nithin chinni
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, when i was playing with synchronization I came across this issue. I Created 2 threads in a class and then in the run method where I start my thread, I created an object whose 2 methods are synchronized and called each method from other method.


When I was going through "Head First Java 2nd edition" in page 511(Topic object synchronization) its mentioned that when a thread obtains a lock from the object because the methods are synchronized it will not give access to any other threads entering any other synchronized methods of the same object. But my example did not do like that.

Second issue, consider when one thread is accessing one method and other thread is accessing other method until one thread finishes that method execution it should not all other thread to execute the same method but this is not happening. My Code and outputs are shown below.









Output Code:

Start Implementation of method 1 by t thread
Start Implementation of method 2 by t1 thread
Main thread wakes
End Implementation of method 2 by t1 thread
Start Implementation of method 1 by t1 thread
End Implementation of method 1 by t thread
Start Implementation of method 2 by t thread
End Implementation of method 2 by t thread
Start Implementation of method 1 by t thread
End Implementation of method 1 by t1 thread
Start Implementation of method 2 by t1 thread
End Implementation of method 1 by t thread
Start Implementation of method 2 by t thread
End Implementation of method 2 by t1 thread
Start Implementation of method 1 by t1 thread
End Implementation of method 2 by t thread
Start Implementation of method 1 by t thread
End Implementation of method 1 by t1 thread
Start Implementation of method 2 by t1 thread




Here In the output code 5th line after t1 thread ends in method2 it starts method 1 before thread t can end method 1. But both method are synchronized.
Hope I found something useful for everyone...........

Forgot to say something..... if I put synchronized before run method in class "RunClass" it works as in the way it is supposed to work. So does that mean if we want to make a class object work in a synchronized way we have to make sure the method in which we create or assign the object should be synchronized. So in that case we have to create an object of that class only in a method because if we create that object outside the method like a variable it does not work because classes cannot be synchronized.

Does java has automated deadlock prevention or if its in a deadlock situation does it break synchronization rule?
 
Henry Wong
author
Sheriff
Posts: 22853
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nithin chinni wrote:
When I was going through "Head First Java 2nd edition" in page 511(Topic object synchronization) its mentioned that when a thread obtains a lock from the object because the methods are synchronized it will not give access to any other threads entering any other synchronized methods of the same object. But my example did not do like that.


The key phrase in that sentence is "same object", if you follow the code, you will see that the two threads are using two different instances of the MyRunnableObject class.

Henry
 
Henry Wong
author
Sheriff
Posts: 22853
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nithin chinni wrote:
Forgot to say something..... if I put synchronized before run method in class "RunClass" it works as in the way it is supposed to work.


In this second case, it is the same instance as you only have one instance of the RunClass class.

Henry
 
nithin chinni
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
nithin chinni wrote:
When I was going through "Head First Java 2nd edition" in page 511(Topic object synchronization) its mentioned that when a thread obtains a lock from the object because the methods are synchronized it will not give access to any other threads entering any other synchronized methods of the same object. But my example did not do like that.


The key phrase in that sentence is "same object", if you follow the code, you will see that the two threads are using two different instances of the MyRunnableObject class.

Henry


Oh you got that right mate...........At times I am crazy. I am trying to use the same instance to both the threads and have no idea how to do it. Can you help me with that too.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!