java | creating Thread and calling its static method


//Example of Thread and its method
//Thread class is available in  java.lang.*; package which is call by compiler by default

class ThreadDemo
{

public static void  main(String ar[])//main is a thread which start very first
{

Thread t=Thread.currentThread();//currentThread() is static method available in Thread class

System.out.println("Current thread= "+ t );//will print thread name,priority,group name      //(main,5,main)

// changing thread priority and its name

t.setPriority(1);//by Default priority is 5 (Normal)   10=highest  1= lowest
t.setName("MyThread");

System.out.println("Current thread= "+t);

// to see priority and name of thread

System.out.println("priority= "+t.getPriority());
System.out.println("Name= "+t.getName());
System.out.println("IsAlive = "+t.isAlive());//it returns true /false.

try{

for(int i=0;i<=5;i++)
{
System.out.println("i= "+ i );
t.sleep(1000);//  1000 means. 1 second pause
}
}
catch(InterruptedException e)
{

System.out.println("main Thread interrupted ");
}
}




}

Reactions:

0 comments:

Post a Comment