Pages

Multithreading in java

What is Multithreading in java?

Before going to learn multithreading we discuss Multitasking.

Multitasking:: Executing multiple tasks simultaneously
Example: A person is eating and simultaneously the person is listening to songs.

Advantage: Main objective of multi tasking is to reduce response time of systems and to increase performance of a system

---> Multitasking divided into 2 parts:
1) Process based
2) Thread based

Multitasking
Multitasking


1) Process based: 
-->Executing several tasks simultaneously where each task separate independent process (i.e., first process doesn't depend on other process)
-->Process based is best suitable at OS level
example:: 
-->writing a program, simultaneously listening songs from same system,simultaneously download a file from internet.
-->In a operating system,several applications can run like Notepad,paint,youtube as they are all independent processes.

2) Thread based:
-->Executing several tasks simultaneously where each task is separate independent part of same program.
-->Thread based is best suitable at programmatic level.
example::
--> Executing multiple parts of a program simultaneously.
-->To develop web servers and application servers etc.,.internally multi threading concept will be used.
ex: facebook server,youtube,gmail servers can be accessed by many multiple requests.
Thread::
--> Flow of execution or light weight or independent job called as a thread.
-->Running of multiple threads simultaneously called Multi threading

We can define a thread in following 2 ways:
1) By extending thread class
2) By implementing Runnable interface
Creation of Threads
Creation of Threads
Lets see how Threads can be implemented by extending thread class.
Demo on extending thread class:
public class ThreadDemo extends Thread { public void run() { for (int i = 0; i <= 10; i++) { //executed by child thread System.out.print("Child Thread "); //executed by child thread try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) throws InterruptedException { //here main thread starts ThreadDemo td = new ThreadDemo(); //Thread Instantiation //main thread created child thread object td.start(); //Starting of a thread //here child thread starts for (int i = 0; i <= 10; i++) { Thread.sleep(2000); System.out.print("Main Thread "); //executed by main thread } } } If we execute above program we will get mixed outputs.i.e., we will get different output every we execute the program. Output:
Child Thread Child Thread Main Thread Main Thread Child Thread Child Thread Main Thread Main Thread Child Thread Child Thread Main Thread
Main Thread Child Thread Main Thread Child Thread Main Thread Child Thread Child Thread Main Thread Main Thread Child Thread Main Thread
Demo on implementing Runnable interface:
public class ThreadDemo implements Runnable {
    public void run() {
        for (int i = 0; i <= 5; i++) { //executed by child thread
            System.out.print("child Thread "); //executed by child thread
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) throws InterruptedException {
        //here main thread starts
        ThreadDemo td = new ThreadDemo();//main thread creates object (td)
        Thread t1 = new Thread(td);
        t1.start(); //child thread will be created
        //here there are 2 threads
        for (int i = 0; i <= 5; i++) {
            Thread.sleep(1000);
            System.out.print("main Thread "); //executed by main thread
        }
    }
}
By executing above program, we will get mixed outputs and we can't tell exact output. Output:
child Thread main Thread child Thread main Thread child Thread child Thread main Thread main Thread child Thread child Thread main Thread main Thread --> A class which implements Runnable Interface must be passed as an argument to the thread object. --> To call the run() method, we use start() method. Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.