Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Thread functioning help

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);
public void run(){
int i=0;
System.out.println("thread is : "+ Thread.currentThread()+" running ");

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.
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic