It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread functioning  help 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 "Thread functioning  help" Watch "Thread functioning  help" New topic

Thread functioning help

Hamraj Kulshreshtha
Ranch Hand

Joined: Oct 06, 2007
Posts: 31
In the below program why all the threads are running even if exception appears in the first thread.
As per my thinking the program should terminate after the first thread and no more thread should run.

public class Threadtest implements Runnable{
public static void main(String args[]){
Threadtest threadtest = new Threadtest();
Thread t1 = new Thread(threadtest);
Thread t2 = new Thread(threadtest);
Thread t3 = new Thread(threadtest);
Thread t4 = new Thread(threadtest);
public void run(){
int i=0;
System.out.println("thread is : "+ Thread.currentThread()+" running ");


SCJP 1.5 91%
Serg Masow
Ranch Hand

Joined: Dec 17, 2007
Posts: 50
Hi Hamraj,

an uncaught Exception just terminates the thread where it was thrown, but not the main thread. Therefore your code shows many exception thrown, one exception for each thread.

SCJP 6.0 [95%] OCE EJBD 6.0 [93%]
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331

Its not that if an exception occurs in one thread, it should necessarily occur in another thread too..

See this-

Now an exception can occur only in the thread that first executes the run()... So why should the language be designed to end other threads too.. Needless, rite.

[ January 16, 2008: Message edited by: Vishwanath Murthi ]

I agree. Here's the link:
subject: Thread functioning help
It's not a secret anymore!