MultithreadingLecture
MultithreadingLecture
TECHNOLOGY
Process based :Executing several task simultaneously where each task is a separate Independent
Process and is not dependent on each other for example : Typing a java program in Editor while
typing ,students listens audio song from the same system and at the same time download some file from
Internet .Every activity is an Independent process such an activity is called Process based ,where each
task is a separate Independent process and all are executing simultaneously and all are Independent
process (no dependency)this is the concept of Process based (Process based is considered at OS
level whereas at Programmer level we consider Thread based (Each task is a separate Independent
application, Multiple Independent Programs will be there)
Thread Based:
As we know that class consist of methods variables and constructors so here Thread class consist of a number of
Methods and to access methods of Threads we have to create object of a class that we called as Thread
RUNNING—if processor is available then it move to the running state if the thread is allocated to the processor
then it starts execution
If any I/O interruption occur, then thread move to the waiting or BLOCKED STATE
After Completion of I/O Event again it will move to the RUNNING STATE or It may move to the RUNNABLE state
If processor available, it moves to the running state or if the processor is busy it moves to the runnable state
If no interruption occurs and after execution of the process it will move to the stop state ie termination
FIVE STATES of THREAD
• NEW
• RUNNABLE
• RUNNING
• WAITING/BLOCKED
• STOP
Thread class have number of Methods so by accessing those methods this thread will change its
state
RUNNING
BLOCKED – A thread that is blocked waiting for a monitor lock is in this state.
t.start();//starting of a thread
for(int i=0;i<10;i++);
{
System.out.println("Main Thread");}
}
//This part will be executed by Main Thread
//Save both the program in one file and save it with thread2 .java
Javac thread2.java
Java thread2
In multithreading we cannot expect the output that whether main thread will execute first or
child thread but all the threads run simultaneously
TWO WAYS