Skip to main content

Thread

MULTITHREADING

Multitasking:-Executing several task simultaneously is known as multitasking, there are two types of multitasking.
1. Process Based Multitasking
2. Thread Based Multitasking 

Process Based Multitasking:-Executing several task simultaneously where each task is separate independent program (Process) is called process based multitasking.
Ex-:                              While typing a java program in the editor
                                    Same time we can listen audio from the same system
                                    At the same time we can download your file.
All these task will be executed simultaneously and independent each other hence it is process based multitasking.

Note:-It is best suitable for Operating System (O.S) level.

 Thread Based Multitasking:-Executing several tasks simultaneously where each task separate and independent part of same program (Process) is called Thread Based Multitasking.
And each independent part is called a Thread.
Q1.What is the Thread?
Ans. Thread is a separate flow of execution.

Defining a Thread:-
We can define a thread in the following two ways.
1. By Extending Thread Class.
2. By Implementing Runnable Interface.
By Extending Thread Class:-

Case1-:Thread Scheduler 

It is the part of JVM .It is responsible to schedule Threads. If multiple thread are waiting to get chance of execution then in which order threads will be executed is decided by thread scheduler.
We can’t accept exact algorithm followed by thread Scheduler it varied from JVM to JVM .Hence we can’t accept threads execution order and exact Output. 
Whenever situations comes to multithreading there is no guarantee exact output but we can provide several output.


            class MyThread extends Thread{
            public void run(){
                        for(int i=0;i<10;i++){
                        System.out.println("Child Thread");
                        }
            }
}
public class ThreadDemo{
            public static void main(String[] args){
            MyThread t = new MyThread();
            t.start();
                        for(int i=0;i<10;i++){
                        System.out.println("main Thread");
                        }
            }
}

Case2:-Difference Between t. start() and t.run();[Impotant]

In the case of t.start() a new thread will be created which is responsible of run method.
But in the case of t.run() a new thread does not created and run method will be executed just like a normal method call by main Thread.
In above program if you replace t.start() with t.run() then the no Thread will be created and total output produce by main Thread.

Note:-main method is executed by the main Thread.

Case3:-Important of thread class start() method

Thread class start method is responsible to register the thread with thread Scheduler and all other mandatory activities.
Hence without executing thread class start() method there are no chance starting a new thread in Java.
Due to this thread  class start() method is consider as heart of  multithreading.
                        Start(){
                                    1.Register this thread with thread Scheduler
                                    2.Perform all other mandatory activities.
                                    3.Invoke run();
            }
Case4:- Overload run() method

Yes,We can overload run method.
Overloading of run method is always possible but thread class start() method can invoke no-argument run() method the other overloaded method we have to call explicitly like a normal method call.
Ex-                                           class MyThread extends Thread {
                                                            Public void run(){
                                                                        System.out.println(“No arg Run”);
                                                            }
                                                            Public void run(int i){
                                                                        System.out.println(“int arg Run”);
                                                            }
                                                }
                                                class ThreadDemo{
                                                            public static void main(String [] arg ){
                                                            MyThread t  = new MyThread();
                                                            t.start();
                                                            }}
 Case5:- Overriding run() method

If you are not overriding run () method then thread class  run() method will be executed .which has empty implementation .
Hence we don’t get any output.
Note:-so it is highly recommended to override run() method otherwise don’t go for multithreading.
Case6:-Override start() method :-
If we override the start() method then  our start() method will be executed just like a normal method call no thread will be start().
                        class MyThread extends Thread {
                                                            Public void start(){
                                                                        System.out.println(“start method”);
                                                            }
                                                            Public void run(){
                                                                        System.out.println(“run method”);
                                                            }
                                                }
class ThreadDemo{
                                                            public static void main(String [] arg ){
                                                            MyThread t  = new MyThread();
                                                            t.start();
                                                            t.run();
                                                            }
}
Output:-                                   start method
                                                run method      [this output provided by main thread]

Note:-It is not recommended to override start() method otherwise don’t go multithreading concept.
Case7:-            In above example ,in override start() method we use super.start()  statement then define  thread will be start.
                                    class MyThread extends Thread {
                                                            Public void start(){
                                                                        Super.start();//this line call thread class start method
                                                                        System.out.println(“start method”);
                                                            }
                                                }
class ThreadDemo{
                                                            public static void main(String [] arg ){
                                                            MyThread t  = new MyThread();
                                                            t.start();
                                                            }
}
Case8:- Thread Life Cycle


Case9:- After starting a thread if we are trying to restart the same thread then we will runtime exception.
            Runtime Exception :- Illegal Thread State Exception  ;
Ex:-                  MyThread t = new MyThread();
                        t.start();
                        :

                        t.start();//This line generate Illegal Thread State Exception .

Comments

Popular posts from this blog

The Best Web Hosting Services

Are you looking for the best web  hosting  services for your needs? Whether you need a place to host your small personal blog or a major corporate website, the following list will help you identify the best hosts to use. Finding the best web hosting service isn’t quite as straightforward as searching Google and choosing the one with the lowest price. There are a lot of issues to consider, including the reasons for  why  you need hosting and  how  you intend to use it. Once you have a handle on that, finding the right host becomes much easier. Choose one that’s undersized and you’ll end up with website outages and slow page loads, but choose one that’s oversized and you’ll be throwing money away. Defining Your Web Hosting Needs Before choosing your web host, you’ll need to think about your requirements. Consider the following concerns and decide the importance of each item on a scale of 0 to 10 (with 0 being not at all important and 10 being critically important): Speed  — H

Google Photos can now stabilize all your shaky phone camera video

G oogle Photos is where all my photos are. Long ago I was a man of SmugMug, and then Flickr, and then at some point spent days and days copying years of images to iCloud Photo Library before eventually disregarding that and switching to Google. What can I say? I’m a simple person who can be easily delighted and swayed by automatic GIF creation and reliable backups. And Google Photos keeps getting better. Here’s the latest example: now the mobile app can automatically stabilize videos in your camera roll with a tap. A lot of flagship smartphones offer optical image stabilization when shooting video, a hardware feature that helps keep footage smooth. Others, like Google’s Pixel, use software to try and stabilize jerky movements. Putting stabilization inside the Google Photos app could enhance results further if you’re already working with hardware OIS, or improve recordings significantly if your phone lacks any means of steadying things out of the box. The stabilized video is croppe