aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads using two different instances Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads using two different instances Problem" Watch "Threads using two different instances Problem" New topic
Author

Threads using two different instances Problem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634





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


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

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

Joined: Feb 24, 2006
Posts: 6662
    
    5

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.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634


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

Joined: Feb 24, 2006
Posts: 6662
    
    5

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

Joined: May 18, 2010
Posts: 634

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

Joined: Feb 24, 2006
Posts: 6662
    
    5

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

Joined: Aug 03, 2008
Posts: 9305
    
  17

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"...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

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
 
subject: Threads using two different instances Problem