• 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

Thread question repuired

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class A extends Thread {
A() {
setDaemon(true);
}
public void run() {
(new B()).start();
try {
Thread.sleep(60000);
} catch (InterruptedException x) {}
System.out.println("A done");
}

class B extends Thread {
public void run() {
try {
Thread.sleep(60000);
} catch (InterruptedException x) {}
System.out.println("B done");
}
}

public static void main(String[] args) {
(new A()).start();
}
}
The result is "There is no exception that the application will print anything."
I compile it and it compile successfully.
But there is no output.
So I think it should be "There is no exception that the application will print nothing."
Right??
 
PETER CARTER
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
another question 1:What does "setDaemon(true);" mean ?

another question 2:"If in run() we use start() again,it will throw IllegalMonitorStateException ."
We look at :
public void run() {
(new B()).start();
}
as before .
So I think it should compile fails!
But why not ??


Thanks !!!
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I think it should be "There is no exception that the application will print nothing."

correct unless somebody interrupts the thread.



Originally posted by PETER CARTER:
another question 1:What does "setDaemon(true);" mean ?

another question 2:"If in run() we use start() again,it will throw IllegalMonitorStateException ."
We look at :
public void run() {
(new B()).start();
}
as before .
So I think it should compile fails!
But why not ??


Thanks !!!



1. There are two types of threads user and daemon. from
http://www.absolutejava.com/main-articles/beware-the-daemons/

java makes a distinction between a user thread and another type of thread known as a daemon thread. The difference between these two types of threads is straightforward: If the Java runtime determines that the only threads running in an application are daemon threads (i.e., there are no user threads in existence) the Java runtime promptly closes down the application, effectively stopping all daemon threads dead in their tracks. In order for an application to continue running, it must always have at least one live user thread. In all other respects the Java runtime treats daemon threads and user threads in exactly the same manner.


2. It will only throw IllegalMonitorStateException if you call start more than once on the same instance of the thread. In the above example you are creating two differnt instances. Even if you did the code will compile fine, but will throw an IllegalMonitorStateException during runtime.
[ September 18, 2004: Message edited by: Benjamin Gunawardana ]
 
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic