• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt with the Daemon thread

 
amithk kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the below program i have created a thread and made it as daemon thread using setDaemon(true) and created Two String objects when ever i run this the console shows this output

main thread=non daemon thread
before for s1 0
before for s2 0




public class DaemonTest {
public static void main(String[] args){
Runnable obj = new Runnable()
{

String s1=new String();
String s2=new String();
public void run()
{
System.out.println(" s1 "+s1.hashCode());
System.out.println(" s2 "+s2.hashCode());

try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
System.out.println("inside run");
}
};
Thread t = new Thread(obj);
//t.setDaemon(false);
t.setDaemon(true);
t.start();
System.out.println("main thread=non daemon thread");
}
}


1)why s1 and s2 hashcode value is zero

2) after JVM exits what happens to the s1 and s2 string objects will they be collected by garbage collector ?
 
Paul Clapham
Sheriff
Posts: 21869
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amithk kumar wrote:1)why s1 and s2 hashcode value is zero


Because that's the hashcode of those two strings. Why shouldn't it be zero?

2) after JVM exits what happens to the s1 and s2 string objects will they be collected by garbage collector ?


No, after the JVM exits nothing at all happens. The variables and objects all cease to exist, the application no longer exists, and so on. There would be no point in pausing for the garbage collector to clean up any unreferenced objects at that point because there isn't going to be any more processing that could reuse that collected memory space.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!