What is Multithreading, multithreading is multi tasking feature in which multiple tasks are running in same time, the advantage of multithreading is increase CPU utilization and improve performances.
Multithreading is basically multiple threads, so question is what thread, a thread is sub part of process or we can say it is subset of single process and so here is another advantage that thread is light weight or it can give you same advantages which processes can give you. You will hear the term of Multiprocessing, the purpose of Multiprocessing and Multithreading is same but both are very different things.
![]() |
Multithreading in java |
There is problem in using multithreading, actually all the threads are running independently, if one thread is depended on another thread (i mean output of one thread is input another thread ) then in such there will be huge problem, in order solve this problem and make all threads collaborating with each other, I will give simple solution of this problem.
The solution is simple i need to add thread in waiting queue ( waiting state ) , when it is needed i will resume the thread (running state) and take task from it. But question how can i do it , will i use Thread.Sleep(), the answer is no, Thread.Sleep long pause that thread for milliseconds and no one knows the exact figure of milliseconds that thread will sleep. The solution can be obtain from two things.
First i need to use monitor in java for synchronization of threads and other i need to use Object for blocking and adding the thread in waiting state.
Theoretical part is done , now lets do it practically. Suppose i have class named clock , i need to make clock class a thread so i simply extends it with Thread and add two required functions
1. Start()
2. Run()
public class Clock extends Thread {
String name;
Thread t;
public Clock(String name){
this.name = name
}
}
Download Problem pdf file (Requirements) Download Java Multi threading project
Theoretical part is done , now lets do it practically. Suppose i have class named clock , i need to make clock class a thread so i simply extends it with Thread and add two required functions
1. Start()
2. Run()
public class Clock extends Thread {
String name;
Thread t;
public Clock(String name){
this.name = name
}
public void run(){
System.out.print("Thread started");
}
public void start(){
t = new Thread( (Runnable)this,name);
}
}
Above is simple code for creating class as thread. Now how to synchronize and lock the thread as waiting. In java, there is built in function for monitor that is Synchronize( Any_Object_to_synchornize ){} and for locking, i will use Object. Example is below
lock.wait() : it will add the thread in waiting queue
lock.notify() : it will release or resume thread where it is paused.
I have added source code of multithreading project, download project below
Object lock = new Object();
Synchronize( lock ){// add thread execution code here}
lock.wait() : it will add the thread in waiting queue
lock.notify() : it will release or resume thread where it is paused.
I have added source code of multithreading project, download project below
Download Problem pdf file (Requirements) Download Java Multi threading project