aspose file tools*
The moose likes Threads and Synchronization and the fly likes output with Thread Class and Runnable interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "output with Thread Class and Runnable interface" Watch "output with Thread Class and Runnable interface" New topic
Author

output with Thread Class and Runnable interface

sr shashidhar
Greenhorn

Joined: Jan 17, 2013
Posts: 27
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

Joined: Aug 07, 2007
Posts: 2318
    
  49
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

Joined: Jan 17, 2013
Posts: 27
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

Joined: Aug 07, 2007
Posts: 2318
    
  49
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

Joined: Jan 17, 2013
Posts: 27

Nice explanation Tony Docherty, Thanks.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2318
    
  49
My pleasure
 
Don't get me started about those stupid light bulbs.
 
subject: output with Thread Class and Runnable interface