Multithreading in Java – What is Java Multithreading?

Multithreading in Java- An Introduction

In Java, Multithreading refers to a process of executing two or more threads simultaneously for maximum utilization of the CPU. A thread in Java is a lightweight process requiring fewer resources to create and share the process resources.

Multithreading and Multiprocessing are used for multitasking in Java, but we prefer multithreading over multiprocessing. This is because the threads use a shared memory area which helps to save memory, and also, the content-switching between the threads is a bit faster than the process.

Few more advantages of Multithreading are:

  • Multithreading saves time as you can perform multiple operations together.
  • The threads are independent, so it does not block the user to perform multiple operations at the same time and also, if an exception occurs in a single thread, it does not affect other threads.

Life Cycle of a Thread

There are five states a thread has to go through in its life cycle. This life cycle is controlled by JVM (Java Virtual Machine). These states are:

  1. New
  2. Runnable
  3. Running
  4. Non-Runnable (Blocked)
  5. Terminated

1. New

In this state, a new thread begins its life cycle. This is also called a born thread. The thread is in the new state if you create an instance of Thread class but before the invocation of the start() method.

2. Runnable

A thread becomes runnable after a newly born thread is started. In this state, a thread would be executing its task.

3. Running

When the thread scheduler selects the thread then, that thread would be in a running state.

4. Non-Runnable (Blocked)

The thread is still alive in this state, but currently, it is not eligible to run.

5. Terminated

A thread is terminated due to the following reasons:

  • Either its run() method exists normally, i.e., the thread’s code has executed the program.
  • Or due to some unusual errors like segmentation fault or an unhandled exception.

A thread that is in a terminated state does not consume ant cycle of the CPU.

Java Thread Class

The Java Thread class provides methods and constructors to create and perform operations on a thread. The Java thread class extends the Object class and implements the Runnable interface.

Java Thread Methods

These are the methods that are available in the Thread class:

1. public void start()

It starts the execution of the thread and then calls the run() on this Thread object.

Example:

    
    public void run()  
        
        System.out.println("Thread is running...");    
        
    public static void main(String args[])  
        
        StartExp1 thread1=new StartExp1();
        thread1.start();    
        
  

Output:

Thread is running…

2. public void run()

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:

public class RunExp1 implements Runnable  
    
    public void run()  
        
        System.out.println("Thread is running...");    
        
    public static void main(String args[])  
        
        RunExp1 r1=new RunExp1();    
        Thread thread1 =new Thread(r1);    
        thread1.start();    
        
  

Output:

Thread is running…

3. public static void sleep()

This blocks the currently running thread for the specified amount of time.

Example:

public class SleepExp1 extends Thread  
    
    public void run()  
        
        for(int i=1;i<5;i++)  
            
            try  
              
                Thread.sleep(500);  
            catch(InterruptedException e)System.out.println(e);    
            System.out.println(i);    
            
        
    public static void main(String args[])  
        
        SleepExp1 thread1=new SleepExp1();    
        SleepExp1 thread2=new SleepExp1();    
        thread1.start();    
        thread2.start();    
        
  

Output:

1

1

2

2

3

3

4

4

4. public static Thread currentThread()

It returns a reference to the currently running thread.

Example:

public class CurrentThreadExp extends Thread  
    
    public void run()  
        
        System.out.println(Thread.currentThread().getName());    
        
    public static void main(String args[])  
        
        CurrentThreadExp thread1=new CurrentThreadExp();    
        CurrentThreadExp thread2=new CurrentThreadExp();    
        thread1.start();    
        thread2.start();    
        
  

Output:

Thread-0

Thread-1

5. public void join()

It causes the current thread to block until the second thread terminates or the specified amount of milliseconds passes.

Example:

public class JoinExample1 extends Thread  
    
    public void run()  
        
        for(int i=1; i<=4; i++)  
            
            try  
                
                Thread.sleep(500);    
            catch(Exception e)System.out.println(e);    
            System.out.println(i);    
            
        
    public static void main(String args[])  
       
        JoinExample1 thread1 = new JoinExample1();    
        JoinExample1 thread2 = new JoinExample1();    
        JoinExample1 thread3 = new JoinExample1();    
        thread1.start();   
       try  
            
        thread1.join();    
        catch(Exception e)System.out.println(e);    
        thread2.start();   
        thread3.start();    
        
  

Output:

1

2

3

4

1

1

2

2

3

3

4

4

6. public final int getPriority()

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:

public class JavaGetPriorityExp extends Thread  
    
    public void run()  
        
        System.out.println("running thread name is:"+Thread.currentThread().getName());    
        
    public static void main(String args[])  
        
        JavaGetPriorityExp t1 = new JavaGetPriorityExp();    
        JavaGetPriorityExp t2 = new JavaGetPriorityExp();    
        System.out.println("t1 thread priority : " + t1.getPriority());   
        System.out.println("t2 thread priority : " + t2.getPriority());  
        t1.start();    
        t2.start();  
        
  

Output:

t1 thread priority : 5

t2 thread priority : 5

running thread name is:Thread-0

running thread name is:Thread-1

7. public final void setPriority()

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:

public class JavaSetPriorityExp1 extends Thread  
    
    public void run()  
        
        System.out.println("Priority of thread is: "+Thread.currentThread().getPriority());    
        
    public static void main(String args[])  
        
        JavaSetPriorityExp1 t1=new JavaSetPriorityExp1();
        t1.setPriority(Thread.MAX_PRIORITY);    
        t1.start();    
        
 

Output:

Priority of thread is: 10

8. public final String getName()

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:

public class GetNameExample extends Thread  
    
    public void run()  
        
        System.out.println("Thread is running...");    
        
    public static void main(String args[])  
       
        // creating two threads   
        GetNameExample thread1=new GetNameExample();    
        GetNameExample thread2=new GetNameExample();    
        System.out.println("Name of thread1: "+ thread1.getName());    
        System.out.println("Name of thread2: "+thread2.getName());    
        thread1.start();    
        thread2.start();    
        
  

Output:

Name of thread1: Thread-0

Name of thread2: Thread-1

Thread is running…

Thread is running…

9. public final void setName()

This method changes the name of the thread.

Example:

public class SetNameExample extends Thread  
    
    public void run()  
        
        System.out.println("running...");    
        
    public static void main(String args[])  
       
        SetNameExample thread1=new SetNameExample();    
        SetNameExample thread2=new SetNameExample();    
        thread1.start();    
        thread2.start();       
        thread1.setName("Kadamb Sachdeva");    
        thread2.setName("Great learning");  
        System.out.println("After changing name of thread1: "+thread1.getName());  
        System.out.println("After changing name of thread2: "+thread2.getName());  
        

Output:

After changing name of thread1: Kadamb Sachdeva

After changing name of thread2: Great Learning

running…

running…

10. public long getId()

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:

public class GetIdExample extends Thread  
    
    public void run()  
        
        System.out.println("running...");    
        
    public static void main(String args[])  
        
        GetIdExample thread1=new GetIdExample();    
        System.out.println("Name of thread1: "+thread1.getName());  
        System.out.println("Id of thread1: "+thread1.getId());   
        thread1.start();  
        

Output:

Name of thread1: Thread-0

Id of thread1: 21

running…

11. public final boolean isAlive()

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:

public class JavaIsAliveExp extends Thread   
  
    public void run()  
      
        try   
          
            Thread.sleep(300);  
            System.out.println("is run() method isAlive "+Thread.currentThread().isAlive());  
          
        catch (InterruptedException ie)   
          
      
    public static void main(String[] args)  
      
        JavaIsAliveExp thread1 = new JavaIsAliveExp();  
        System.out.println("before starting thread isAlive: "+thread1.isAlive());  
        thread1.start();  
        System.out.println("after starting thread isAlive: "+thread1.isAlive());  
      
  

Output:

before starting thread isAlive: false

after starting thread isAlive: true

is run() method isAlive true

12. public static void yield()

This method pauses the execution of the current thread to execute other threads temporarily.

Example:

public class JavaYieldExp extends Thread  
  
    public void run()  
      
        for (int i=0; i<3 ; i++)  
            System.out.println(Thread.currentThread().getName() + " in control");  
      
    public static void main(String[]args)  
      
        JavaYieldExp thread1 = new JavaYieldExp();  
        JavaYieldExp thread2 = new JavaYieldExp();  
        thread1.start();  
        thread2.start();  
        for (int i=0; i<3; i++)  
          
            thread1.yield();  
            System.out.println(Thread.currentThread().getName() + " in control");  
          
      
  

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

13. public final void suspend()

This method is used to suspend the currently running thread temporarily. Using the resume() method, you can resume the suspended thread.

Example:

public class JavaSuspendExp extends Thread  
    
    public void run()  
        
        for(int i=1; i<5; i++)  
            
            try  
              
                 sleep(500);  
                 System.out.println(Thread.currentThread().getName());    
            catch(InterruptedException e)System.out.println(e);    
            System.out.println(i);    
            
        
    public static void main(String args[])  
        
        JavaSuspendExp thread1=new JavaSuspendExp ();    
        JavaSuspendExp thread2=new JavaSuspendExp ();   
        JavaSuspendExp thread3=new JavaSuspendExp ();
        thread1.start();  
        thread2.start();  
        thread2.suspend();   
        thread3.start();  
        
  

Output:

Thread-0

1

Thread-2

1

Thread-0

2

Thread-2

2

Thread-0

3

Thread-2

3

Thread-0

4

Thread-2

4

14. public final void resume()

This method is used to resume the suspended thread. It is only used with the suspend() method.

Example:

public class JavaResumeExp extends Thread  
    
    public void run()  
        
        for(int i=1; i<5; i++)  
            
            try  
              
                 sleep(500);  
                 System.out.println(Thread.currentThread().getName());    
            catch(InterruptedException e)System.out.println(e);    
            System.out.println(i);    
            
        
    public static void main(String args[])  
        
        JavaResumeExp thread1=new JavaResumeExp ();    
        JavaResumeExp thread2=new JavaResumeExp ();   
        JavaResumeExp thread3=new JavaResumeExp ();   
        thread1.start();  
        thread2.start();  
        thread2.suspend();
        thread3.start();   
        thread2.resume();
        
  

Output:

Thread-0

1

Thread-2

1

Thread-1

1

Thread-0

2

Thread-2

2

Thread-1

2

Thread-0

3

Thread-2

3

Thread-1

3

Thread-0

4

Thread-2

4

Thread-1

4

15. public final void stop()

As the name suggests, this method is used to stop the currently running thread. Remember, once the thread execution is stopped, it cannot be restarted.

Example:

public class JavaStopExp extends Thread  
    
    public void run()  
        
        for(int i=1; i<5; i++)  
            
            try  
              
                sleep(500);  
                System.out.println(Thread.currentThread().getName());    
            catch(InterruptedException e)System.out.println(e);    
            System.out.println(i);    
            
        
    public static void main(String args[])  
        
        JavaStopExp thread1=new JavaStopExp ();    
        JavaStopExp thread2=new JavaStopExp ();   
        JavaStopExp thread3=new JavaStopExp ();   
        thread1.start();  
        thread2.start();  
        thread3.stop();  
        System.out.println("Thread thread3 is stopped");    
        
  

Output:

16. public void destroy()

This thread method destroys the thread group as well as its subgroups.

Example:

public class JavaDestroyExp extends Thread   
  
    JavaDestroyExp(String threadname, ThreadGroup tg)  
      
        super(tg, threadname);  
        start();  
      
    public void run()  
      
        for (int i = 0; i < 2; i++)   
          
            try  
              
                Thread.sleep(10);  
              
            catch (InterruptedException ex)   
                System.out.println("Exception encounterted");  
          
        System.out.println(Thread.currentThread().getName() +  
              " finished executing");  
      
    public static void main(String arg[]) throws InterruptedException, SecurityException  
      
        ThreadGroup g1 = new ThreadGroup("Parent thread"); 
        ThreadGroup g2 = new ThreadGroup(g1, "child thread");  
        JavaDestroyExp thread1 = new JavaDestroyExp("Thread-1", g1);  
        JavaDestroyExp thread2 = new JavaDestroyExp("Thread-2", g1);  
        thread1.join();  
        thread2.join();  
        g2.destroy();  
        System.out.println(g2.getName() + " destroyed");  
        g1.destroy();  
        System.out.println(g1.getName() + " destroyed");  
      
  

Output:

Thread-1 finished executing

Thread-2 finished executing

child thread destroyed

Parent thread destroyed

17. public final boolean isDaemon()

This thread method will check if the thread is a daemon thread or not. If it is a daemon thread, then it will return true else, it will return false.

For those who don’t know about a daemon thread, a daemon thread is a thread that will not stop the Java Virtual Machine (JVM) from exiting when the program is ended, but the thread is still running.

Example:

public class JavaIsDaemonExp extends Thread  
    
    public void run()  
        
        //checking for daemon thread    
        if(Thread.currentThread().isDaemon())  
          
            System.out.println("daemon thread work");    
            
        else  
            
            System.out.println("user thread work");    
            
        
    public static void main(String[] args)  
        
        JavaIsDaemonExp thread1=new JavaIsDaemonExp();   
        JavaIsDaemonExp thread2=new JavaIsDaemonExp();    
        JavaIsDaemonExp thread3=new JavaIsDaemonExp();    
        thread1.setDaemon(true);  
        thread1.start();   
        thread2.start();    
        thread3.start();    
        
  

Output:

daemon thread work

user thread work

user thread work

18. public final void setDaemon(boolean on)

This method of a thread is used to identify or mark the thread either daemon or a user thread. The JVM automatically terminates this thread when all the user threads die.

This thread method must run before the start of the execution of the thread.

Example:

public class JavaSetDaemonExp1 extends Thread  
    
    public void run()  
        
        if(Thread.currentThread().isDaemon())  
          
            System.out.println("daemon thread work");    
            
        else  
            
            System.out.println("user thread work");    
            
        
    public static void main(String[] args)  
        
        JavaSetDaemonExp1 thread1=new JavaSetDaemonExp1();   
        JavaSetDaemonExp1 thread2=new JavaSetDaemonExp1();    
        JavaSetDaemonExp1 thread3=new JavaSetDaemonExp1();    
        thread1.setDaemon(true);  
        thread1.start();   
        thread2.setDaemon(true);  
        thread2.start();    
        thread3.start();    
        
   

Output:

daemon thread work

daemon thread work

user thread work

19. public void interrupt()

This method of a thread is used to interrupt the currently executing thread. This method can only be called when the thread is in sleeping or waiting state.

But if the thread is not in the sleeping or waiting state, then the interrupt() method will not interrupt the thread but will set the interrupt flag to true.

Example:

public class JavaInterruptExp1 extends Thread  
    
    public void run()  
        
        try  
            
            Thread.sleep(1000);    
            System.out.println("javatpoint");    
        catch(InterruptedException e)    
            throw new RuntimeException("Thread interrupted..."+e);  
              
            
        
    public static void main(String args[])  
        
        JavaInterruptExp1 thread1=new JavaInterruptExp1();    
        thread1.start();    
        try  
            
            thread1.interrupt();    
        catch(Exception e)System.out.println("Exception handled "+e);    
        
    

Output:

Exception in thread “Thread-0” java.lang.RuntimeException: Thread interrupted…java.lang.InterruptedException: sleep interrupted at JavaInterruptExp1.run(JavaInterruptExp1.java:10)

20. public boolean isInterrupted()  

This thread method is used to test whether the thread is interrupted or not. It will return the value of the internal flag as true or false, i.e. if the thread is interrupted, it will return true else, it will return false.

Example:

public class JavaIsInterruptedExp extends Thread   
   
    public void run()   
       
        for(int i=1;i<=3;i++)   
           
            System.out.println("doing task....: "+i);   
           
       
    public static void main(String args[])throws InterruptedException   
       
        JavaIsInterruptedExp thread1=new JavaIsInterruptedExp();   
        JavaIsInterruptedExp thread2=new JavaIsInterruptedExp();   
        thread1.start();   
        thread2.start();  
        System.out.println("is thread interrupted..: "+thread1.isInterrupted());  
        System.out.println("is thread interrupted..: "+thread2.isInterrupted());  
        thread1.interrupt();   
        System.out.println("is thread interrupted..: " +thread1.isInterrupted());   
        System.out.println("is thread interrupted..: "+thread2.isInterrupted());   
      
  

Output:

is thread interrupted..: false

is thread interrupted..: false

is thread interrupted..: true

is thread interrupted..: false

doing task….: 1

doing task….: 2

doing task….: 3

doing task….: 1

doing task….: 2

doing task….: 3

21. public static boolean interrupted()

This thread method is used to check if the current thread is interrupted or not. If this threading method is to be called twice in succession, then the second call will return as false.

If the interrupt status of the thread is true, then this thread method will set it to false. 

Example:

public class JavaInterruptedExp extends Thread   
   
    public void run()   
       
        for(int i=1;i<=3;i++)   
           
            System.out.println("doing task....: "+i);   
           
       
    public static void main(String args[])throws InterruptedException   
       
        JavaInterruptedExp thread1=new JavaInterruptedExp();   
        JavaInterruptedExp thread2=new JavaInterruptedExp();   
        thread1.start();   
        thread2.start();  
        System.out.println("is thread thread1 interrupted..:"+thread1.interrupted()); 
        thread1.interrupt();   
        System.out.println("is thread thread1 interrupted..:"+thread1.interrupted());   
        System.out.println("is thread thread2 interrupted..:"+thread2.interrupted());   
      
  

Output:

is thread thread1 interrupted..: false

is thread thread1 interrupted..: false

is thread thread2 interrupted..: false

doing task….: 1

doing task….: 2

doing task….: 3

doing task….: 1

doing task….: 2

doing task….: 3

22. public static int activeCount()

This method of the thread is used to return the no. of active threads in the currently executing thread’s thread group.

The number returned by this threading method is only an estimate number as the number of threads dynamically changes while this method traverses internal data structures.

Example:

public class JavaActiveCountExp extends Thread   
  
    JavaActiveCountExp(String threadname, ThreadGroup tg)  
      
        super(tg, threadname);  
        start();  
      
    public void run()  
      
       System.out.println("running thread name is:"
+Thread.currentThread().getName());    
      
    public static void main(String arg[])  
      
        ThreadGroup g1 = new ThreadGroup("parent thread group");  
          JavaActiveCountExp thread1 = new JavaActiveCountExp("Thread-1", g1);  
        JavaActiveCountExp thread2 = new JavaActiveCountExp("Thread-2", g1);  
          System.out.println("number of active thread: "+ g1.activeCount());  
      
  

Output:

number of active thread: 2

running thread name is: Thread-1

running thread name is: Thread-2

23. public final void checkAccess()

This thread method identifies if the current thread has permission to modify the thread.

Example:

public class JavaCheckAccessExp extends Thread     
    
    public void run()  
      
        System.out.println(Thread.currentThread().getName()+" finished executing");  
      
    public static void main(String arg[]) throws InterruptedException, SecurityException    
       
        JavaCheckAccessExp thread1 = new JavaCheckAccessExp();    
        JavaCheckAccessExp thread2 = new JavaCheckAccessExp();    
        thread1.start();  
        thread2.start();  
        thread1.checkAccess();    
        System.out.println(t1.getName() + " has access");    
        thread2.checkAccess();    
        System.out.println(t2.getName() + " has access");    
        
  

Output:

Thread-0 has access

Thread-1 has access

Thread-0 finished executing

Thread-1 finished executing

24. public static boolean holdsLock(Object obj)

This thread method checks if the currently executing thread holds the monitor lock on the specified object. If it does, then this threading method will return true.

Example:

public class JavaHoldLockExp implements Runnable   
  
    public void run()   
      
        System.out.println("Currently executing thread is: " + Thread.currentThread().getName());  
        System.out.println("Does thread holds lock? " + Thread.holdsLock(this));  
        synchronized (this)   
          
            System.out.println("Does thread holds lock? " + Thread.holdsLock(this));  
          
      
    public static void main(String[] args)   
      
        JavaHoldLockExp g1 = new JavaHoldLockExp();  
        Thread thread1 = new Thread(g1);  
        thread1.start();  
      
  

Output:

Currently executing thread is: Thread-0

Does thread holds lock? false

Does thread holds lock? true

There are various thread methods that are used for different tasks and purposes. Those thread methods are as follows:

  • public static void dumpStack()
  • public StackTraceElement[] getStackTrace()
  • public static int enumerate(Thread[] tarray)
  • public Thread.State getState()
  • public final ThreadGroup getThreadGroup()
  • public String toString()
  • public final void notify()
  • public final void notifyAll()
  • public void setContextClassLoader(ClassLoader cl)
  • public ClassLoader getContextClassLoader()
  • public static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler()
  • public static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)

Thread Creation

While multithreading in Java, you can create a thread using two ways:

  1. By extending Thread class
  2. By implementing the Runnable interface

What is Thread Class?

Thread class provides the methods and constructors to create and perform operations on a thread. Thread class extends Object class and implements the Runnable interface.

Various constructors are used in a Thread class, but the commonly used constructors are:

  • Thread()
  • Thread(String name)
  • Thread(Runnable r)
  • Thread(Runnable r,String name)

Also, as discussed earlier, there are various thread methods that are used for different purposes and tasks.

So, these constructors and methods are provided by the Thread class to perform various operations on a thread. 

What is a Runnable Interface?

Runnable Interface is implemented whose instances are intended to be executed by a thread. It has only one method run().

public void run() – This is used to perform an action for a thread.

Starting a Thread

While multithreading in Java, to start a newly created thread, the start() method is used. 

  • A new thread starts(with a new callstack).
  • The thread moves from the New state to the Runnable state.
  • When the thread gets a chance to execute, its target run() method will run.

Java Thread Example by extending Thread Class

class Multi extends Thread  
public void run()  
System.out.println("thread is running...");  
  
public static void main(String args[])  
Multi thread1=new Multi();  
thread1.start();  
   
 

Output:

thread is running…

Java Thread Example by implementing Runnable interface

class Multi3 implements Runnable  
public void run()  
System.out.println("thread is running...");  
  

public static void main(String args[])  
Multi3 m1=new Multi3();  
Thread thread1 =new Thread(m1);  
thread1.start();  
   
  

Output:

thread is running…

So, this was the basic understanding of Multithreading in Java. This brings us to an end of this blog. Hope this helped you understand Multithreading in Java better and gain more insights into it.

Check out our blog on Inheritance in Java to understand inheritance concepts better. To learn more about programming and other related concepts, check out the courses on Great Learning Academy

Also, do check out our top data science course to up-skill in the field of Data Science and power ahead.

Have a Great Learning!

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: