This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread question repuired Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread question repuired" Watch "Thread question repuired" New topic
Author

Thread question repuired

PETER CARTER
Ranch Hand

Joined: Aug 28, 2004
Posts: 70
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

Joined: Aug 28, 2004
Posts: 70
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 !!!
Inuka Vincit
Ranch Hand

Joined: Aug 10, 2004
Posts: 175
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 ]

MCP (C# application dev 70-316) 860<br />SCJP 1.4 100% SCJD (URLyBird) 378<br />MAD 100% nuts
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread question repuired
 
Similar Threads
setDaemon method
Deamon Threads
Doubt on setDeamon().
Thread Question.
Tell me the output and how u go it please....