Thursday, 18 September 2014

JAVA PROGRAM TO CREATE A THREAD AND CHANGING ITS NAME

program:
========================================
import java.io.*;
public class thread2
{
public static void main(String[] args)
{
Thread t=Thread.currentThread();
System.out.println("curent thread : "+t);
t.setName("my thread");
System.out.println("curent changed thread : "+t);
}
}
=======================================
save this as thread2.java( filename.java)
file name must be same as  class name
To compilation command is $ javac filename.java( Here it is thread2.java)
To run : $ java filename

STEP1:
in this program reference to the current thread(the main thread in this case) is obtained by calling currentThread().
The name of the current thread is saved in variable t.

STEP2:
 System.out.println("curent thread : "+t);
This statement will display the name of that thread.

STEP3:
 t.setName("my thread");
This statement will change the name of the thread to "my thread".

STEP4:
System.out.println("curent changed thread : "+t);
this will print the name of the the thread


output will appear in the following syntax:
Thread[thread_name,its priority,name_of_its_group]
bt default the priority of main thread is 5.




No comments:

Post a Comment

Total Pageviews