• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

output with Thread Class and Runnable interface

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nice explanation Tony Docherty, Thanks.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My pleasure
 
And inside of my fortune cookie was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic