Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wait and sleep/JVM queries

 
kushi kumar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Following are some questions i faced in an interview, any views on these is appreciated

1)When I was googling, One of the main differences btwn wait() and sleep() is that wait() releases the lock/object whereas sleep takes the object with itself and goes into sleep mode. I just have a doubt that in case Thread.sleep() is called from a unsynchronized block, does it take this object or it does not take any obj...

2)Say for e.g i execute java a and then java b, does "a" run in 1 jvm and "b" runs in another jvm?

Cheers,
kushi
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kushi kumar wrote:1)When I was googling, One of the main differences btwn wait() and sleep() is that wait() releases the lock/object whereas sleep takes the object with itself and goes into sleep mode. I just have a doubt that in case Thread.sleep() is called from a unsynchronized block, does it take this object or it does not take any obj...

Saying that Thread.sleep() 'takes the object with it' is misleading. Thread.sleep() simply doesn't release any locks. So if it is called in a thread which holds locks, those locks continue to be help. If it is called in a thread which does not hold locks, none are taken.

kushi kumar wrote:2)Say for e.g i execute java a and then java b, does "a" run in 1 jvm and "b" runs in another jvm?

Not sure I follow you. Do you mean if you open a command prompt, type in java a to start a Java application, then type in java b to start a second application (assuming you used the correct syntax so that the first application can continue to run without blocking the command prompt), are you asking if the two different java applications are run in different JVMs? They are run in different, unrelated processes, so yes, I would probably call that two different JVMs.
 
kushi kumar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Steve-->Thanks.. You clarified my doubts... Can we make 2 programs to use the same jvm.. If so in which scenario it will be helpful?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kushi kumar wrote:@Steve-->Thanks.. You clarified my doubts... Can we make 2 programs to use the same jvm.. If so in which scenario it will be helpful?

You can't make two different applications run in the same JVM. It wouldn't be helpful, and would almost certainly be detrimental because they would share memory space, classloaders, and other things that would lead to contention.

That said, you could have a single launcher app that creates threads, and each thread could load and run a static main() from someplace so it would appear to be running multiple applications. But you would really be only running one program (the launcher) and your other 'programs' running inside it could have unexpected problems.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic