Multi-threading in Java (source code example)





Multi-threading in java is a process of executing multiple threads simultaneously.
Thread is basically a lightweight sub-process, a smallest unit of processing. Multiprocessing and multi-threading, both are used to achieve multitasking.
But we use multi-threading than multiprocessing because threads share a common memory area. They don't allocate separate memory area so saves memory, and context-switching between the threads takes less time than process.
Java Multi-threading is mostly used in games, animation etc.

Advantages:
1) It doesn't block the user because threads are independent and you can perform multiple operations at same time.
2) You can perform many operations together so it saves time.
3) Threads are independent so it doesn't affect other threads if exception occur in a single thread.


Source Code:
package multiThreading;

//Class no: 1
class Thread1 extends Thread {

        @Override
        public void run() {
       
       
                for (int i = 1; i <=100; i++) {
                       
                        System.out.println("t1: "+i);
                        try {
                                Thread.sleep(100);
                        } catch (InterruptedException e) {
               
                                e.printStackTrace();
                        }
                }              
        }

}

//Class no: 2
class Thread2 extends Thread {

        public void run() {
               
                                for (int i = 1; i <=100; i++) {
                       
                        System.out.println("t2: "+i);
                        try {
                                Thread.sleep(100);
                        } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
       
}
}

//Class no3
class Thread3 extends Thread {

        public void run() {
               
       
                for (int i = 1; i <=100; i++) {
                       
                        System.out.println("t3: "+i);
                        try {
                                Thread.sleep(100);
                        } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
}
}

// the main class
public class Main {

        public static void main(String[] args) {
               
                // creating objects of the class namely Thread 1, thread 2 and Thread 3
        Thread1 t1= new Thread1();
        Thread2 t2= new Thread2();
        Thread3 t3= new Thread3();

// check with stop watch
// t1, t2 and t3 each need 11sec to complete the counting
       
        //so in the UniThreading (where each thread run in a sequence) needs 11+11+11 = 33 sec to complete the counting of of t1, t2 and t3)  
        // for uniThreading
        System.out.println("Thread No:1");
        t1.run();
        System.out.println("\nThread No:2");
        t2.run();
        System.out.println("\nThread No: 3");
        t3.run();
       

        // in the MultiThread (where each thread run parallel) needs 11 sec only to complete the counting of t1, t2 and t3
        // for multiThreading
       
        t1.start();
        t2.start();
        t3.start();

// total time to run the program is : (UniThreading =33 sec) +( MultiThreading= 11) = 44sec (test with stop watch)
       
        }
       
       
}


Comments

Popular posts from this blog

Umbrella activities in Software Engineering

Operating System | Best Definition of Opetating System