Thread

Hi Everybody, Can someone help me regarding these questions. Question1: Which of these statements are true? A) Running user threads prevent JVM from terminating a program. B) Daemon threads can't be grouped together. C) Daemon threads can't be destroyed. D) Running Daemon threads prevent JVM from terminating a program. I am sure that A and C correct but i want to know if B is also correct. Question2: Which of the following condition will cause the thread to stop? A) sleep (long ms) B) wait () C) notify () D) suspend () E) stop () F) run method ends I think Answers are E and F. If the question is - Which of these will cause the thread to stop executing. The answer will be A, B, D, E and F. Please tell me if I am correct! Thanks Kiran.

Which of these statements are true? A) Running user threads prevent JVM from terminating a program. // true the main thread does not terminate till all user threads have completed their job and return back even though the main method has finished executing. B) Daemon threads can't be grouped together. // false Daemon threads can be grouped together. this can be done by creating a thread group and by setting one or more threads under this thread groups isDaemon(boolean t) property to true. A ThreadGroup can have both user threads and Daemon threads.

C) Daemon threads can't be destroyed. \\true By destroyed i understand stopped. Java 1.2 has deprecated stop() and the suspend() method. As such a thread cannot be "destroyed". It has to undertake the work it is supposed to do. However note that A daemen thread ceases to exist once a user thread have completed all their work. Thus the JVM explicitly has to stops the work of the daemon thread when all user threads have completed. D) Running Daemon threads prevent JVM from terminating a program. //false With regard to Question 2 I think the answer is correct. Execution of a thread will stop if for example sleep() is called(to be started again later). Regds. Rahul

Hi rahul, just to clear my doubt. from ur explanation given for option C shall I understand that any thread be it user/daemon can't be destroyed. deekasha

Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1864

posted Sep 14, 2000 04:07:00

0

Deekasha, destroy does not have any implementation in the thread class and the method stop has been deprecated. You can thus not stop ( by which i mean end) a thread now. The thread can be either a user or a daemon thread. However you can request a thread to stop by using interrupt() . The interrupt() method my not stop the thread instantly as the thread may be in a while loop where in it checks this status and exits if this is true. example public void run(){ while(Thread.isInterrupted()){ //Will stop only the next time this work is completed and the while loop is reentered // } However you can implement something similar to interrupt by keeping a boolean variable in your runnable instance and setting this to true when you want to stop this thread. example public class st implements Runnable{ boolean b; // false by default public void run(){ while (b){// keep running while b is set to true } } this b variable can be declared as public or private and set to true directly by instance.b=true from another thread or by creating a setter method like this private b; setb(){ b=true; } Hope i have clarified adequately ------------------ Regds. Rahul P. Mahindrakar

[This message has been edited by Rahul Mahindrakar (edited September 14, 2000).]