javatutelearn.com

Easy Learning


Home

Search Javatutelearn.com :



Java Multithreading

  • Thread is a small program that can be run simultaneously with other programs.
  • Thread is lightweight process because it has little overheads when it is running.
  • Therefore, a thread can be run simultaneously with other threads.
  • When many threads are running at the same time, it is called multithreading.
  • Threads encourages parallel processing that leads to increase response time and increase utilization time of cpu.

There are two methods to create threads in java.

  • Method 1. By using extending Thread class
  • Method 2. By using implementing the Runnable interface

Method 1: - By using extending Thread class

  • In this method, the class extends Thread class
  • Consider following program:
class T1 extends Thread
{
	public void run()
	{
		try {
				for(int n = 5; n > 0; n--) {
				System.out.println("Child Thread" + n);
				Thread.sleep(1000);
				}
			}catch (Exception e) {
				System.out.println("Child thread interrupted");
				}
	}
}

Explanation of the program

  • This program shows a class named T1 which is extended from the class Thread.
  • Class T1 overrides run() method . It is called when a thread starts.
  • To start a thread, create the instance of class T1 as given below:
class Test
{
          public static void main(String args[])
           {		
					try {
							T1 a =new T1();
							a.start();
                    
							for(int n = 5; n > 0; n--) {
								System.out.println("main thread"+n);
								Thread.sleep(1000);
							}
						} catch (Exception e) {
							System.out.println("Main thread interrupted");
		
						}
           }
		  
}
  • The above example shows three important things:
  • Thread class: - This class is extended by the class T1 for including multithreading features in the class.
  • run() method:- This method is overridden by the class T1 . This method consists of functionality which should be executed by the thread.
  • start() method:- This method is used to start a thread.

    Method 2. Implementing the Runnable interface

    • In this method, A class implements Runnable interface
    • Consider the following program:
    class T1 implements Runnable
    {
    	public void run()
    	{
    		try {
    				for(int n = 5; n > 0; n--) {
    				System.out.println("Child Thread" + n);
    				Thread.sleep(1000);
    				}
    			}catch (Exception e) {
    				System.out.println("Child thread interrupted");
    				}
    	}
    }
    class Test1
    {
              public static void main(String args[])
               {		
    					try {
    							T1 a =new T1();
    							Thread t = new Thread(a);
    							t.start();
                        
    							for(int n = 5; n > 0; n--) {
    								System.out.println("main thread"+n);
    								Thread.sleep(1000);
    							}
    						} catch (Exception e) {
    							System.out.println("Main thread interrupted");
    		
    						}
               }
    		  
    }
    
    • Class T1 implements Runnable interface for implementing multithreading concepts in the class T1 .
    • Method run() is overridden by class T1 .
    • Class Test creates instances of class T1 i.e. object a .
    • Object a is wrapped with the object of Thread class i.e. t .
    • Object t is used to start a thread by calling start() method.

    Example: Creating three threads

    public class T1 implements Runnable
    {
    	public void run()
    	{
    		try {
    				for(int n = 5; n > 0; n--) {
    				//System.out.println("Child Thread" + n);
    		        Thread t = Thread.currentThread();
                     String name = t.getName();
                    System.out.println("name=" + name +n);
    				Thread.sleep(1000);
    				}
    			}catch (InterruptedException e) {
    				System.out.println("Child thread interrupted");
    				}
    	}
        public static void main(String args[])
        {		
    					try {
    							T1 a1 =new T1();
    							Thread t1 = new Thread(a1, "First Thread");					
    							t1.start();
    
    							T1 a2 =new T1();
    							Thread t2 = new Thread(a2, "Second Thread");
    							t2.start();
                        
    							T1 a3 =new T1();
    							Thread t3 = new Thread(a3, "Third Thread");
    							t3.start();
                       
    							for(int n = 5; n > 0; n--) {
    								System.out.println("main thread"+n);
    								Thread.sleep(1000);
    							}
    						} catch (InterruptedException e) {
    							System.out.println("Main thread interrupted");
    		
    						}
        }
    		  
    }
    

    Output

    name=First Thread5
    name=Third Thread5
    name=Second Thread5
    main thread5
    name=First Thread4
    name=Second Thread4
    main thread4
    name=Third Thread4
    name=First Thread3
    main thread3
    name=Second Thread3
    name=Third Thread3
    name=First Thread2
    main thread2
    name=Second Thread2
    name=Third Thread2
    name=First Thread1
    name=Second Thread1
    main thread1
    name=Third Thread1
    


© Copyright 2016-2024 by javatutelearn.com. All Rights Reserved.