• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

output with Thread Class and Runnable interface

 
sr shashidhar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This following 2 programs are implemented with the same logic i.e, In one program, creating thread by extending Thread class and the other by implementing Runnable interface.

Here the output will be like "Print some values till i press ENTER or RETURN key", My problem is, when i run these two programs "program where we are creating Thread by extending Thread class will by halted from
extending Thread class printing values till i Press ENTER/RETURN but "program where we are creating Thread with Runnable interface output is getting halted"

Here the logic is same in these two programs, only difference is one program extending Thread Class and implementing Runnable interface.


Creating Thread by extending Thread Class





Creating Thread by Implementing Runnable Interface




can anyone explain about output with above code.

Thanks in advance
 
Tony Docherty
Bartender
Posts: 2953
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference is in the first case you create the threads as daemon threads and in the second case you don't.
The JVM will only exit when all non-daemon threads have finished and because, in the second case, you have started 3 threads that are not daemon threads the JVM does not exit and the 3 threads keep on running.
 
sr shashidhar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:The JVM will only exit when all non-daemon threads have finished and because, in the second case, you have started 3 threads that are not daemon threads the JVM does not exit and the 3 threads keep on running.


I didn't got what this sentence actually mean, can you elaborate on this...
 
Tony Docherty
Bartender
Posts: 2953
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JVM enters its normal shutdown sequence when the last non-daemon thread terminates (or System.exit() is called).

In the first case the main thread creates three daemon threads, starts them and waits until ENTER is pressed. The main thread then returns from the main method and terminates. Because the only other threads running are daemon threads the JVM shuts down and the daemon threads are stopped.

In the second case the main thread creates three threads, starts them and waits until ENTER is pressed. The main thread then returns from the main method and terminates. But the three threads are not daemon threads and so the JVM continues as normal and the threads continue to run.
 
sr shashidhar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nice explanation Tony Docherty, Thanks.
 
Tony Docherty
Bartender
Posts: 2953
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My pleasure
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic