All the three questions originates from practicing the JQPlus mock exam: 1. Daemon thread never stops. (True?) 2. Can a user thread stop a daemon thread? (False?) 3. I thought that a transient variable cannot be final and a transient variable cannot be static. However, public final static transient int i = 10; does not give compilation error. Is my concept on "transient" wrong?
Amond Adams
Ranch Hand
Joined: Nov 28, 2000
Posts: 62
posted
0
1. Daemon threads do not Stop (false) JVM exists when no user threads are running and it does'nt care to wait for Daemon threads, stoping them as it exists. 2. A user thread can stop a daemon thread. (true) Consider the following piece of code.... class D{ public static void main(String[] args){ DT t1 = new DT(); t1.setDaemon(true); t1.start(); try{ Thread.sleep(1); } catch(Exception e){} t1.stop(); System.out.println("Done..."); } } class DT extends Thread{ public void run(){ for(int i=0; i<100;i++) System.out.println(Thread.currentThread().getName() + " says " + i); } } Run the code and observe.... 3. yes you can declare a variable to be final static transient int i Perhaps you mixed it up with volatile where volatile variables cannot be final and vice versa, but they can be static.
Santhosh Kumar
Ranch Hand
Joined: Nov 07, 2000
Posts: 242
posted
0
there is another catch here. Interface member variables can't be volatile. Because variables defined in Interface are by-default static and final. But volatile variables can't be final. So volatile variables are not allowed in interfaces. Santhosh.