This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread functioning help

 
Hamraj Kulshreshtha
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ");
}

}
 
Serg Masow
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic