Friday, April 12, 2019

current thread name

public class cl extends Thread{ 
 public void run(){ 
  for(int i=1;i<=3;i++){ 
   try{ 
    Thread.sleep(500); 
   }
   catch(Exception e){System.out.println(e);} 
   System.out.print(Thread.currentThread().getName()+" "); 
  System.out.println(i); 
  } 
 } 
public static void main(String args[]){ 
 cl t1=new cl(); 
 cl t2=new cl(); 
 cl t3=new cl(); 


  System.out.println("Name of t1:"+t1.getName()); 
  System.out.println("Name of t2:"+t2.getName());
  System.out.println("Name of t1:"+t3.getName());
  System.out.println("id of t1:"+t1.getId());
  System.out.println("id of t1:"+t2.getId());
  System.out.println("id of t1:"+t3.getId());
 t1.start(); 
 try{ 
  t1.join(); 
 }catch(Exception e){System.out.println(e);} 
 
   t1.setName("dj thread"); 
   System.out.println("Name of t1:"+t1.getName());
 
 t2.start(); 
 t3.start(); 


 } 




$javac cl.java
$java -Xmx128M -Xms16M cl
Name of t1:Thread-0
Name of t2:Thread-1
Name of t1:Thread-2
id of t1:21
id of t1:22
id of t1:23
Thread-0 1
Thread-0 2
Thread-0 3
Name of t1:dj thread
Thread-1 1
Thread-2 1
Thread-1 2
Thread-2 2
Thread-1 3
Thread-2 3

No comments:

Post a Comment