File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question 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 "question on threads" Watch "question on threads" New topic
Author

question on threads

JayaSiji Gopal
Ranch Hand

Joined: Sep 27, 2004
Posts: 303
Few q:

1) when the yield, sleep or join() called on a thread, shd they be necessarily within the try catch construct? any compile time error?

2) can a thread unyield itself?

3) can a thread unjoin itself?


SCJP 1.4, SCWCD 1.4<br /> <br />Thanks in advance!<br />Jayashree.
Inuka Vincit
Ranch Hand

Joined: Aug 10, 2004
Posts: 175
Originally posted by JayaSiji Gopal:
Few q:

1) when the yield, sleep or join() called on a thread, shd they be necessarily within the try catch construct? any compile time error?

2) can a thread unyield itself?

3) can a thread unjoin itself?


1)
join and sleep yes. Because they throw InterruptedException which is a checked exception. All checked exceptions must be caught or a compile time error is produced. Join() doesnt throw any such exception so therefore you dont need to do anything.

2) 3) no although an other thread can call the interrupt() method. I dont see how a thread can do anything while it isnt doing anything.

see definition for Thread

http://java.sun.com/j2se/1.3/docs/api/java/lang/Thread.html

exceptions java spec
http://java.sun.com/docs/books/jls/second_edition/html/exceptions.doc.html
[ November 26, 2004: Message edited by: Inuka Vincit ]

MCP (C# application dev 70-316) 860<br />SCJP 1.4 100% SCJD (URLyBird) 378<br />MAD 100% nuts
Nikhil Menon
Ranch Hand

Joined: Nov 22, 2004
Posts: 70
Hi,

(1)Since the two methods namely sleep() and join()throws InterruptedException which is a checked exception, it either needs to be caught and handled or should be again thrown that is the only alternative.

(2)About unyielding, once a thread decides to be yielded by invoking the static method Thread.yield() then it is under the mercy of the thread scheduler. i.e. the CPU time would be alloted to that thread when the scheduler decides and in no way the thread unyield itself.

(3)Unjoining, here when the thread imvokes the join() then it is pushed to a "non runnable state". So there is no question of the thread to perform any operation including unjoining!!
(a) if the joined thread runs to completion
(b) if the specified time expires(see the argument of join(long ms))
(c) if interrupted by any other thread by invoking interrupt() on that
thread.

Remember I) By Thread.yield(), the thread moves to "ready to run
(Runnable state)" and not "nonrunnable state".

(II) And join() will push the thread to "nonrunnablestate".

Nikhil Kanjulli Menon.
SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question on threads