• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about threads, using locks of the objects in synchronized blocks

 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was attempting small programs about the threads acquiring the objects of the locks
I did not take it from any source so can't quote it

well what I understood from the program is given in the comments
I want to check whether I am right or not
here is the code and the comments


the output of the program is

F:\Java\Concepts\Threads\source files>java ThreadsWithManyLocks
1 Thread1
2 Thread1
3 Thread1
4 Thread1
5 Thread1

1 Thread3 // is this because of the reason mentioned above?
2 Thread3 // it gets executed before t2
3 Thread3
4 Thread3
5 Thread3

1 Thread2
2 Thread2
3 Thread2
4 Thread2
5 Thread2



but when I change the program a little bit
when we write following code in the main method
then the output is not consistent even if we have synchronized run method


is this because of following reason?

When we write synchronized block of the object, then the threads those are instantiated using the SAME object CANNOT enter synchronized block
and the threads that are NOT created using the same object are allowed to execute before the method completes
here
the thread executing the main method is not the created using the object of the SAME class
hence main thread can run without completion of the synchronized method?
here is the output


F:\Java\Concepts\Threads\source files>java ThreadsWithManyLocks
1 Thread1
2 Thread1
3 Thread1
main0
4 Thread1
main1
5 Thread1
main2

main3
main4
main5
1 Thread2
2 Thread2
3 Thread2
4 Thread2
5 Thread2

1 Thread3
2 Thread3
3 Thread3
4 Thread3
5 Thread3




here both the threads Thread1 and main thread are executing simultaneously
but when we join the main thread at the end of thread t1 as follows


then we get the following output
F:\Java\Concepts\Threads\source files>java ThreadsWithManyLocks
1 Thread1
2 Thread1
3 Thread1
4 Thread1
5 Thread1

1main
2main
3main
4main
5main
1 Thread2
2 Thread2
3 Thread2
4 Thread2
5 Thread2

1 Thread3
2 Thread3
3 Thread3
4 Thread3
5 Thread3

but I am not able to understand why the thread2 in this case did not get executed but in the above program
the Thread3 got executed in the synchronized context of t3

please tell me whether my inference are right or not and also tell why the order of output changed in last example

P.S. when I ran the program many times, then the order of output of Thread2 and Thread3 changes

is this because
sometimes Thread2 is in the runnable when start() is invoked, but not yet running and without it going to the running state, the synchrinized block of t3 starts executing,
and sometimes Thread2 is in the runnable and immediately starts running hence it gets completly executed before t3 starts ?
Am I right
sorry for such a loooooooooooooooooong post
just need to clarify all my inferences
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow that's a really long question. I didn't even read it completely. I really can't understand the second output, how did you get it actually?? The code change that you've shown means you've just moved i after Thread.currentThread().getName() in the println statement. That's it, no other change, in that case how does the output contains main0 main1 etc. Also synchronization is a very easy topic. I didn't read your full post so I don't know how you think it works, but if I change your first code a little bit, you might be able to understand things a little better. This is because by looking at your comments, you are predicting the execution of run method of t2 and t3 at the wrong places
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well forget my code
let us talk about the code given by you
I am still not able to understand when a thread actually starts
let me tell what I have inferred from your code
1. First the main thread is executing
2. starting of t1 thread is fine, I can understand it
3. According to my knowledge, when we write synchronised(t2) then the actually running thread i.e. main thread acquires the lock of the object t2 and no one thread of object t2 can access the synchronized code till the execution of thread is completed, in your post you have given that the execution of thread t2 starts at line 22 even if we are calling the start() method at line 16
and I have the same question regarding the thread t3
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Kharkar wrote:3. According to my knowledge, when we write synchronised(t2) then the actually running thread i.e. main thread acquires the lock of the object t2 and no one thread of object t2 can access the synchronized code till the execution of thread is completed,


No, main thread does acquire a lock on t2, and thus no other thread can run any synchronized code on t2 now. But this doesn't require main thread to end before any other thread can call synchronized code on t2. Once the synchronized block in main method ends (at line 22), any other thread can call synchronized methods on t2 object. This is when the thread started at line 16 will be able to call the run method on t2 i.e. at line 22 when the synchronized code ends...
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got your point
now what I can infer is following
please tell me whether I am right or wrong
this is totally about the code given by you



1. The main thread is started at first, no doubt about it
2. Also t1 starts and there is no doubt about it also
3. Now when we write synchronized(t2) : this means : -
Now the thread executing this code i.e. the main thread has acquired the lock of the object t2 and no other thread can enter this synchronized code until the current thread i.e.
the main thread releases the locks of the object t2
4. Now we go to the line t2.start()// this does not start the thread as the code is synchronized on t2 and no other thread in this case thread t2 can enter the synchronized code block as the current thread i.e. main thread has not released the lock yet so the t2.start does not execute immediately
5. Now again the synchronized(t3) means that the main thread has acquired the locks of the multiple threads t2 and t3 and hence no other thread can invoke that synchronized block until the main thread releases the locks .
6. Now at the end of synchronized block of t3 the lock of the t3 object is released by the main thread and hence the thread t3 CAN (not compulsion) start, because we have already called the t3.start() method which was waiting for main thread to release the lock of the t3 object
7. Now again at the end of the synchronized t2 block the lock of the t2 object is released by the main thread and hence the method thread of t2 object can acquire the lock of the object t2 and call the start() method

After executing the program I got two types of outputs
first is

1 Thread1
2 Thread1
3 Thread1
4 Thread1
5 Thread1

t3 will start after this synchronized block is over
t2 will start after this synchronized block is over
1 Thread2
2 Thread2
3 Thread2
4 Thread2
5 Thread2

1 Thread3
2 Thread3
3 Thread3
4 Thread3
5 Thread3



and the second one is


1 Thread1
2 Thread1
3 Thread1
4 Thread1
5 Thread1

t3 will start after this synchronized block is over
t2 will start after this synchronized block is over
1 Thread3
2 Thread3
3 Thread3
4 Thread3
5 Thread3

1 Thread2
2 Thread2
3 Thread2
4 Thread2
5 Thread2



is this because when the locks of the t2 and t3 were released, then there was no guarantee which thread should run first

Am I right now?
thank you for all the replies
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oooops, I didn't see the code properly. Your run method is called on the instance of obj2. What you understood was right, but the run method is synchronized on obj2 not t2 or t3. So the synchronized blocks don't matter much. Try this code
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at this code now
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now look at this code
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the purpose and the difference between the following statements?

synchronized (obj2)
and
synchronized(t2) or synchronized (t2)

 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the above code, the Thread that will call the run method will be t2, but the run method will actually be called on obj2. When we synchronize on t2 in main method, the call to synchronized run method will not be blocked. But when we synchronize on obj2 in main method, the call to synchronized run method will be blocked till the lock on obj2 is released by main thread. Synchronizing on t2 or t3 doesn't have any effect if the run method is synchronized...
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ankit,
thanks for your help
well I will study this topic thoroughly today and then will ask you again if there is some problem
have a nice time to you
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Ankit sir

I have inferred from the program as follows


Line 17 : - the currently executing thread i.e. main thread has acquired the lock of the object t2 and hence any thread cannot enter in the synchronized block until the lock of the t2 is released so the t2 object cannot start the thread using t2.start()
Line 20 : this gets executed as the current thread is main thread
Line 21 : main thread also acquires the lock of the object t3 and hence further disallowing other threads to enter into the synchronized block
Line 22 : this cannot start t3 thread as the main thread has not released the lock yet
Line 25 : here the main thread can release the lock of the object t3, hence the t3 thread CAN start now
Line 28 : here the lock of the object t2 is released and t2 CAN start now

done too much on this
but am I right now?
or still wrong?
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Kharkar wrote:I have inferred from the program as follows


Which program exactly, your own program or any of my program, please specify which code you are talking about...
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was talking about the code provided by you
I edited that post and added the program now
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes your understanding is right. t2 and t3 cannot enter the synchronized block (which contains the loop) till main method releases the lock on them...
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you Ankit sir
this was like a personal tutor (which I never had in my life : ))
thanks a lot
it cleared many of my concepts
I am appearing for exam soon
will let you know when I get certified
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:In the above code, the Thread that will call the run method will be t2, but the run method will actually be called on obj2. When we synchronize on t2 in main method, the call to synchronized run method will not be blocked. But when we synchronize on obj2 in main method, the call to synchronized run method will be blocked till the lock on obj2 is released by main thread. Synchronizing on t2 or t3 doesn't have any effect if the run method is synchronized...



Interesting & knowledge topic, Thanks a lot to both Ankit Garg and Prasad Kharkar for sharing...

My doubt, By synchronizing the Thread object, what we can do? Are there any methods/synchronized methods to be invoked on top of the Threads(t1,t2,t3). I think, there is a only one method, start() which is invoked on top of Thread(t1,t2,t3) (but the run() method also there, most of the time this(run()) method is invoked on targets not on the threads itself), but it is not synchronized. So no need of synchronizing the Thread(t1,t2,t3) object?

Am I correct? Please confirm! I'm expecting the explanation from Ankit Garg.....

Thanks in Advanced~!
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:My doubt, By synchronizing the Thread object, what we can do? Are there any methods/synchronized methods to be invoked on top of the Threads(t1,t2,t3). I think, there is a only one method, start() which is invoked on top of Thread(t1,t2,t3) (but the run() method also there, most of the time this(run()) method is invoked on targets not on the threads itself), but it is not synchronized. So no need of synchronizing the Thread(t1,t2,t3) object?

Am I correct? Please confirm! I'm expecting the explanation from Ankit Garg....


Well anybody else could've also replied to it couldn't they . Anyway, yes synchronizing on the Thread objects don't block any method calls. This is why I added a synchronized block in the run method to synchronize on the Thread object. But that was a fabricated example, if you are using a Runnable class and a Thread instance to call run on that Runnable, then there is no point of synchronizing the run method. If we extend the Thread class to create our own class, then synchronizing the run method will make any effect...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic