• 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

Threads using two different instances Problem

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
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
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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic