Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread functioning  help" Watch "Thread functioning  help" New topic
Author

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);
t1.start();
t2.start();
t3.start();
t4.start();
}
public void run(){
int i=0;
System.out.println(5/i);
i++;
System.out.println("thread is : "+ Thread.currentThread()+" running ");
}

}


SCJP 1.5 91%
Serg Masow
Ranch Hand

Joined: Dec 17, 2007
Posts: 49
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%] OCP EJBD 6.0 [93%]
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
Hi,

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.


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

Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread functioning help
 
Similar Threads
Doubt in join method
Synchronization of run method
how to use join( )
Threads Using Static Method
ThreadUnsafe