Advertisement
Methods | Description |
---|---|
String getName() | Returns the name of thread. |
void setName() | Setting the name of thread. |
int getPriority() | Returns the priority of thread. |
boolean isAlive() | Checks if a thread is alive or dead. |
void start() | Starts the thread and automatically calls the run() method. |
void run() | Entry point for the thread. |
void sleep() | Suspends the execution of a thread for a specific amount of time. |
void join() | Makes a thread wait for another thread to terminate. |
Thread currentThread() | Returns the reference to the currently executing thread. |
public class ThreadExm
{
public static void main(String... ar)
{
Thread th = Thread.currentThread();
System.out.println("Thread information : "+th);
System.out.println("Name of main thread - "+ th.getName());
th.setName("Thread2"); //Setting a new name of the main thread.
System.out.println("Thread's new information " +th);
System.out.println("New name of main thread - "+ th.getName());
}
}
Thread information : Thread[main,5,main]
Name of main thread - main
Thread's new information : Thread[Thread2,5,main]
New name of main thread - Thread2
Advertisement
class ThreadExm
{
public static void main(String... ar)
{
Thread th= Thread.currentThread();
System.out.println("Thread information:"+ th);
System.out.println("Name of the thread - "+ th.getName());
try
{
System.out.println("main thread going to sleep for 5 seconds");
th.sleep(5000); //Sleep method may throw InterruptedException, hence a try-catch block
System.out.println("main thread is awakened and exits");
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
Thread information :Thread[main,5,main]
Name of the thread - main
main thread going to sleep for 5 seconds
main thread is awakened and exits
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement