0% found this document useful (0 votes)
16 views37 pages

MULTITHREADING

The document discusses threading concepts in computing including thread states, context switching between threads, and implementing threads. It provides code examples of creating and running threads in Java.

Uploaded by

TEJAS SAMPAT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views37 pages

MULTITHREADING

The document discusses threading concepts in computing including thread states, context switching between threads, and implementing threads. It provides code examples of creating and running threads in Java.

Uploaded by

TEJAS SAMPAT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

CPU CPU1 CPU2

CPU

main

run

Process 1 Process 2 Process 3 Process 4

GC
begin

body

end

7
Main Thread

start
start start

Thread A Thread B Thread C

Threads may switch or exchange data/results


PC client

Internet
Server
Local Area Network

PDA
In both cases the run() method should be implemented

10
class MyThread extends Thread {
public void run() {
System.out.println(" this thread is running ... ");
}
}
class ThreadEx1 {
public static void main(String [] args ) {
MyThread t = new MyThread();
t.start();
}
}
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("\t From ThreadA: i= "+i);
}
System.out.println("Exit from A");
}
}
class B extends Thread {
public void run()
{
for(int i=6;i<=10;i++)
{
System.out.println("\t From ThreadB: i= "+i);
}
System.out.println("Exit from B");
}
}
class C extends Thread {
public void run()
{
for(int i=11;i<=15;i++) {
System.out.println("\t From ThreadC: i= "+i);
}
System.out.println("Exit from C");
}
}
Newborn
new ThreadExample(); Thread
stop()
thread.start();

Active Runnable run()


Thread Running
Running Dead Thread
method
yield returns

wait() resume()
sleep() notify() stop()
Suspend()

Idle Thread Blocked


Not Runnable
When we create a thread object, the thread is born
and is said to be in newborn state.
The thread is not yet scheduled for running. At this
state we can do only the following things

Newborn

stop();
start();

Runnable Dead Thread


yield()

Runnable Threads
Running Thread
suspend()

resume()

Running Runnable Suspended

2. It has been made to sleep sleep(t)

after(t)

Running Runnable Suspended

3. It has been told to wait until some event occurs


wait

notify

Runnable Waiting
Running
Demo
ThreadMethods
start()
Ready queue

Newly created
threads

Currently executed
thread
I/O operation completes

•Waiting for I/O operation to be completed


•Waiting to be notified
•Sleeping
•Waiting to enter a synchronized section
The main thread is
created with priority
NORM_PRIORITY
Demo
SynThread1.java

You might also like