File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Ques on Threads 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 "Ques on Threads" Watch "Ques on Threads" New topic

Ques on Threads

josephine chen
Ranch Hand

Joined: Oct 29, 2002
Posts: 216
java.lang.Thread utilizes which of the following methods to relinquish the thread instance's processing slot to another thread?
a. sleep()--true
b. wait()//Objects method otherwise true
c. notify() //Objects method otherwise true
How are thread priorities and thread scheduling related?
a. they aren't--they are related
b. it depends on the thread-scheduling algorithm used to
implement the JVM -true
c. thread priority is specified when a thread is to be
executed next. ---false specified on creation
d. thread priority is used to determing the next thread to
execute from the waiting thread pool. -true
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
I think notify() is incorrect in the first question - aside from the fact that it's an Object method (good call), it doesn't actually do anything to relinquish the thread processing slot - it just tells another thread to get ready for when it does relinquish the slot. The thread calling notify() must already have a lock on the object used to invoke notify(), and the thread being notified will need that same lock itself to resume execution (because it could only call wait() from code synchronized on that object). So the notified thread can't do anything until the notifying thread does something else to relinquish the thread - at the very least, it has to exit the synchronized code block. After that it could just finish executing or call Thread.yield() - otherwise it's up to the thread scheduling algorithm to decide when (or if) to switch over to the waiting thread.
I agree with your other answers though.

"I'm not back." - Bill Harding, Twister

Joined: Feb 02, 2000
Posts: 23
Thread priority is set AFTER the thread is created using setPriority() method and not during creation.
Am I right?
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Ah, I didn't notice that. You are correct, ShreeDevi. On creation, it gets default priority (you can't set it yourself on creation). After that, you can set it whenever you want.
Looking back at the original statement:
c. thread priority is specified when a thread is to be executed next.
I'm not really sure what it was meant to mean. You could specify the priority just before it executes, but you don't have to. And in a multithreaded system you often don't really know when a given thread will execute anyway. So I'll say false just because the statement makes no sense.
Tony Alicea

Joined: Jan 30, 2000
Posts: 3226
I had that question wrong in Bill Brogden's JAVA 2 EXAM CRAM book. That's because:
"When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread,"

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
I agree. Here's the link:
subject: Ques on Threads
It's not a secret anymore!