Saturday 12 January 2019

what is multithreading in java


Whatis multithreading in java

Multi threading

Way to define threads

There are two way to define thread in java.
  1. Introduction
  2. The way to define , Instantiate and start thread
  3. Getting and Setting name of thread
  4. Thread priorities
  5. The method to prevent thread execution
·         Yield
·         Join
·         Sleep
  1. Sysncronization
  2. Interthread communication
  3. Deadlock
  4. Daemon threads

Execution several tasks simultaneously is called multitasking.
There two types of multitasking:
1.       Process base multitasking
2.       Thread base multitasking
Example:
Student in classroom
Student
·         Listening
·         Writing
·         Sleeping
·         Watching
·         Reading
·         Noise keeping etc.


1.    Process base multitasking


Execution several tasks simultaneously, where each task is a separate independent process, is called process base multitasking.
Ex. While typing a java program in editor, we can able to listen audio songs by mp3 player in the system, at the same time we can download a file from the net. All these tasks are executing simultaneously and independent to each other. Hence, it is process base multitasking.
Process base multitasking is suitable at O.S. level.
2.    Thread base multitasking
Executing several tasks simultaneously where each task is a separate independent part of the same program is called Thread base multitasking and each independent part is called thread.
It is best suitable for programmatic level.
  • Whether it is process base or thread base, the main objective of multitasking is to improve performance of the system by reducing response time.
  • The main important application areas of the multithreading are developing video game, multimedia graphics, implementing animations etc.
  • Java provide inbuilt support for multithreading by introducing a rich API (Thread , Runnable, ThreadGroup,Threadlocal…) being a programmer we have to know how to use this API and we are not responsible to define that API. Hence, developing multithreading program is very easy when compare to c++.

 

The way to define, Instantiate and start a new thread

We can define thread in following two ways

  • By extending thread class.
  • By implementing Runnable Interface.
Defining thread by extending thread class
class mythread extends Thread
{
                public void run(){
                for(int i=1;i<=10;i++){
                System.out.println(“child thread…”);
}//job of the thread.
}
}
class ThreadDemo{
public static void main (String args[]){
MyThread  t=new MyThread(); // instantiating  thread
t.start(); // starting of the thread.
for(int i=10;i<=10;i++){
                System.out.println(“main Thread”);
}
}
}

Case: 1

Thread Scheduler
  • Whenever multiple thread waiting to get chance for execution which thread will get chance first is decided by thread Scheduler. Whose behavior is java vendor dependent. Hence, we can’t expect exact execution order. Hence, exact out put.
  • Thread scheduler is part of JVM. Due to this unpredictable behavior of thread scheduler we can’t expect exact output of the above program.
Note: whenever the situation comes to Multithreading the guarantee in the behavior is very less. We can tell possible output but not exact.

 Case: 2

Deference between t.start() and t.run()
In the above case of the t.sart() a new thread will be created and thread will be responsible to execute run().
  • But in the case of t.run() no new thread will be created and run method will be execute just like a normal method call.
  • In the above program, if we are replacing t.start() with t.run() the output will be normal as first 10 time child thread and second 10 time main thread.

No comments:

Post a Comment