Thread Methods
Thread Methods
These are the methods that are available in the Thread class:
It starts the execution of the thread and then calls the run() on this Thread object.
Example:
1
2 {
public void run()
3 {
4 System.out.println("Thread is running...");
5 }
6 public static void main(String args[])
7 {
StartExp1 thread1=new StartExp1();
8 thread1.start();
9 }
10 }
11
Output:
Thread is running…
This thread is used to do an action for a thread. The run() method is instantiated if the thread
was constructed using a separate Runnable object.
Example:
Thread is running…
This blocks the currently running thread for the specified amount of time.
Example:
1
2
3 public class SleepExp1 extends Thread
{
4 public void run()
5 {
6 for(int i=1;i<5;i++)
7 {
try
8 {
9 Thread.sleep(500);
10 }catch(InterruptedException e){System.out.println(e);}
11 System.out.println(i);
12 }
}
13 public static void main(String args[])
14 {
15 SleepExp1 thread1=new SleepExp1();
16 SleepExp1 thread2=new SleepExp1();
thread1.start();
17
thread2.start();
18 }
19 }
20
21
Output:
3
3
Example:
1
2 public class CurrentThreadExp extends Thread
3 {
4 public void run()
{
5 System.out.println(Thread.currentThread().getName());
6 }
7 public static void main(String args[])
8 {
CurrentThreadExp thread1=new CurrentThreadExp();
9
CurrentThreadExp thread2=new CurrentThreadExp();
10 thread1.start();
11 thread2.start();
12 }
13 }
14
Output:
Thread-0
Thread-1
It causes the current thread to block until the second thread terminates or the specified
amount of milliseconds passes.
Example:
4
4
It is used to check the priority of the thread. When a thread is created, some priority is
assigned to it. This priority is assigned either by the JVM or by the programmer explicitly
while creating the thread.
Example:
1
2 public class JavaGetPriorityExp extends Thread
3 {
4 public void run()
5 {
System.out.println("running thread name is:"+Thread.currentThread().getName()
6 }
7 public static void main(String args[])
8 {
9 JavaGetPriorityExp t1 = new JavaGetPriorityExp();
JavaGetPriorityExp t2 = new JavaGetPriorityExp();
10
System.out.println("t1 thread priority : " + t1.getPriority());
11 System.out.println("t2 thread priority : " + t2.getPriority());
12 t1.start();
13 t2.start();
14 }
}
15
16
Output:
t1 thread priority : 5
t2 thread priority : 5
This method is used to change the priority of the thread. The priority of every thread is
represented by the integer number from 1 to 10. The default priority of a thread is 5.
Example:
This method of thread class is used to return the name of the thread. We cannot override this
method in our program, as this method is final.
Example:
1
2 public class GetNameExample extends Thread
3 {
4 public void run()
5 {
System.out.println("Thread is running...");
6 }
7 public static void main(String args[])
8 {
9 // creating two threads
10 GetNameExample thread1=new GetNameExample();
GetNameExample thread2=new GetNameExample();
11 System.out.println("Name of thread1: "+ thread1.getName());
12 System.out.println("Name of thread2: "+thread2.getName());
13 thread1.start();
14 thread2.start();
}
15
}
16
17
Output:
Thread is running…
Example:
1
2 public class SetNameExample extends Thread
3 {
4 public void run()
5 {
6 System.out.println("running...");
}
7 public static void main(String args[])
8 {
9 SetNameExample thread1=new SetNameExample();
10 SetNameExample thread2=new SetNameExample();
thread1.start();
11
thread2.start();
12 thread1.setName("Kadamb Sachdeva");
13 thread2.setName("Great learning");
14 System.out.println("After changing name of thread1: "+thread1.getName());
15 System.out.println("After changing name of thread2: "+thread2.getName());
}
16 }
17
18
Output:
running…
running…
It returns the identifier of the thread. The thread ID is a number generated when the thread
was created. This ID cannot be changed during its lifetime. But when the thread is
terminated, the ID can be reused.
Example:
1
2 public class GetIdExample extends Thread
3 {
4 public void run()
{
5 System.out.println("running...");
6 }
7 public static void main(String args[])
8 {
GetIdExample thread1=new GetIdExample();
9
System.out.println("Name of thread1: "+thread1.getName());
10 System.out.println("Id of thread1: "+thread1.getId());
11 thread1.start();
12 }
13 }
14
Output:
Id of thread1: 21
running…
This method checks if the thread is alive. A thread is in the alive state if the start() method of
thread class has been called and the thread has not yet died.
Example:
This method pauses the execution of the current thread to execute other threads temporarily.
Example:
1
2
public class JavaYieldExp extends Thread
3 {
4 public void run()
5 {
6 for (int i=0; i<3 ; i++)
7 System.out.println(Thread.currentThread().getName() + " in control");
}
8 public static void main(String[]args)
9 {
10 JavaYieldExp thread1 = new JavaYieldExp();
11 JavaYieldExp thread2 = new JavaYieldExp();
thread1.start();
12
thread2.start();
13 for (int i=0; i<3; i++)
14 {
15 thread1.yield();
16 System.out.println(Thread.currentThread().getName() + " in control");
}
17 }
18 }
19
20
Output:
main in control
main in control
main in control
Thread-0 in control
Thread-0 in control
Thread-0 in control
Thread-1 in control
Thread-1 in control
Thread-1 in control