Skip to main content Skip to docs navigation
TechSpiderTutorials

Java.lang.Thread Class in Java

On this page

A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

Ways of creating threads

There are two ways to create a new thread of execution.

  1. One is to declare a class to be a subclass of Thread.
    1. create a class to be a subclass of Thread
    2. override the run() method
    3. Create an instance to the class which was created
    4. Start the thread by using the start() in Thread class
    class Test extends Thread  ->(a)
    {	
    			public void run()	-> (b)
    			{
    			}
    }
    	Class demo 
        {
    		Public static void main(String args[])
    		{
    			Test t=new Test()  -> (c)
    			t.start();   ->(d)
    		 }
    }
  2. The other way to create a thread is to declare a class that implements the Runnable interface.
  3. import java.io.*;
    import java.util.*;
    
    class ThreadA  extends Object implements Runnable
    {
      public void run()
       {
        for(int i=1;<5;i++)
    	 {
    	  System.out.println("ThreadA..."+i);
    	   	}
         }
     }
       public class RunnableDemo
        {
    	  public static void main(String artgs[])
    	   {
    			ThreadA ta=new ThreadA();
    	        Thread th=new Thread(ta);
    			th.start();
    
    			for(int i=0;i<5;i++)
    			{
    			 System.out.println("Main...."+i);
    			}
    	    }
     }

Thread Naming

Method getName()

public final String getName()

Returns this thread's name.

Method setName(String name)

public final void setName(String name)

Changes the name of this thread to be equal to the argument name.

import java.io.*;
import java.lang.*;

class ThreadA extends Thread
 {
   public void run()
    {
	  for(int i=0;i<5;i++)
	   {
	    System.out.println("Thread A is.."+i);
	   }
	}
}

class ThreadB extends Thread
 {
   public void run()
    {
	  for(int i=0;i<5;i++)
	   {
	    System.out.println("Thread B is.."+i);
	   }
	}
}

 public class ThreadName
  {
    public static void main(String args[])
	 {
		 ThreadA ta=new ThreadA();
		 ThreadB tb=new ThreadB();
        System.out.println("Thread A..."+ta.getName());
	    System.out.println("Thread B..."+tb.getName());

		 ta.start();
		 tb.start();

		 ta.setName("printThread");
		 tb.setName("scanningThread");

System.out.println("Thread A."+ta.getName());
System.out.println("Thread B.."+tb.getName());

Thread tm=Thread.currentThread();
System.out.println("main.Name."+tm.getName());

	   for(int i=0;i<5;i++)
	     System.out.println("main..."+i);
		}
 }    

Output:

Thread A...
Thread-0
Thread B...Thread-1
Thread A.printThread
Thread B..scanningThread
main.Name.main

Thread Priority

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon

Method setPriority()

public final void setPriority(int newPriority)

Changes the priority of this thread.

Method getPriority()

public final int getPriority()

Returns this thread's priority.

import java.io.*;
import java.lang.*;

class ThreadA extends Thread
 {
   public void run()
    {
	  for(int i=0;i<5;i++)
	   {
    System.out.println("Thread A is.."+i);
	   }
	}
}

class ThreadB extends Thread
 {
   public void run()
    {
	  for(int i=0;i<5;i++)
	   {
    System.out.println("Thread B is.."+i);
	   }
	}
}

 public class ThreadPriority
  {
    public static void main(String args[])
	 {
		 ThreadA ta=new ThreadA();
		 ThreadB tb=new ThreadB();

System.out.println("Thread A Priority..."+ta.getPriority());
System.out.println("Thread B Priority..."+tb.getPriority());

	ta.setPriority(3);
	tb.setPriority(Thread.MAX_PRIORITY);
		 ta.start();
		 tb.start();

   System.out.println("Thread A.Priority.."+ta.getPriority());
   	System.out.println("Thread B.Priority.."+tb.getPriority());

	 Thread tm=Thread.currentThread();
		tm.setPriority(1);
		for(int i=0;i<5;i++)
	     System.out.println("main..."+i);
	}
 }    

Output:


 Thread A Priority...5
Thread B Priority...5
Thread A.Priority..3
Thread B.Priority..10
Thread B is..0
Thread B is..1
Thread B is..2
Thread B is..3
Thread B is..4
main...0
main...1
main...2
main...3
main...4
Thread A is..0
Thread A is..1
Thread A is..2
Thread A is..3
Thread A is..4