This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes wait and sleep/JVM queries Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "wait and sleep/JVM queries" Watch "wait and sleep/JVM queries" New topic
Author

wait and sleep/JVM queries

kushi kumar
Greenhorn

Joined: Sep 14, 2013
Posts: 13
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

Joined: Jan 28, 2003
Posts: 4164
    
  21

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.


Steve
kushi kumar
Greenhorn

Joined: Sep 14, 2013
Posts: 13
@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

Joined: Jan 28, 2003
Posts: 4164
    
  21

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait and sleep/JVM queries
 
Similar Threads
Classic, Hotspot, native, green
threads
stop a thread (help Rahul M. )
Begin threds through links
Threads and Synchronization examples