• 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

question on threads

 
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic