This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP 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