This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads using two different instances Problem

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




i ran the program and got the following output:


jusbeforesleep
Thread-0
main
jusbeforesleep
Thread-1
Thread-1
Thread-0

but in KB BOOK it written: Text in bold
Threads calling non-static synchronized methods in the same class will
only block each other if they're invoked using the same instance. That's
because they each lock on this instance, and if they're called using two different
instances, they get two locks, which do not interfere with each other

 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What makes you think something is going wrong?

As you say, the threads aren't interfering because they are locking on different objects. Which means there's very little you can guarantee about the order things happen in. The output you've given is just one of many possible outputs.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what is your question ? The ordering will not be guaranteed because the synchronization is done on separate objects as noticed by Matthew. Try synchronizing on the same object instance if you would like to see ordered output.

As an added question, how many possible outputs exist if the threads lock on the same object ? Figuring that out will help you at the exam and at work.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

As an added question, how many possible outputs exist if the threads lock on the same object ? Figuring that out will help you at the exam and at work.


if threads lock on same object,then also there can be many outputs as
JVM can choose any of the threads to run first i.e.
main
thread rba
thread rbb


if thread rba runs first ,then rbb cannot run as they both lock on same object but JVM can start main thread and stop rba in between and run it's code i.e.

then ,JVM again chooses rba and executes its statement from where it was stopped.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JVM can choose any of the threads to run first i.e.
main
thread rba
thread rbb


rba and rbb can certainly not run before main.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Bala:rba and rbb can certainly not run before main.






after line 1 rba thread is in runnable state
JVM may choose to run it or main thread line2
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohitkumar gupta wrote:
Deepak Bala:rba and rbb can certainly not run before main.






after line 1 rba thread is in runnable state
JVM may choose to run it or main thread line2


I certainly cannot refute that control can come back to the main thread after rba starts. My point is that you assertion about rba / rbb running before main simply cannot happen
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Deepak is trying to say is that the output can never be

thread rba
thread rbb
main

i.e. "main" will always be displayed before "thread rbb"...
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thread rba
thread rbb
main

i.e. "main" will always be displayed before "thread rbb"..


I too agree but
rbb thread run() may run before rba run() as they have separate locks
JVM may choose thread rbb first

Possible output:

main
rba
rbb

i.e

System.out.println(Thread.currentThread().getName());
then run of either rba or rbb
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic