View
223
Download
1
Category
Tags:
Preview:
Citation preview
Threads
Concurrency in Java
What is mult-tasking?
• Doing more than one task.
What is true concurrency?
• when you do two or more things at the same time.– Network of Workstations– Dual Pentium computer has two processors.– Multi-processor system
classification by Flynn: (No. of instruction and data streams) SISD - conventional SIMD - data parallel, vector
computing MISD - systolic arrays MIMD - very general, multiple
approaches.
Ways of processing
Ideal speedup
0
2
4
6
8
10
12
1 2 3 4 5 6 7 8 9 10
num ofCPUs
IdealSpeedup
typicalspeedup
What is multi-threading?
• When you have multiple threads.
Single and Multithreaded Processes
Single-threaded Process
Single instruction stream Multiple instruction stream
Multiplethreaded ProcessThreads of
Execution
CommonAddress Space
threads are light-weight processes within a process
What is a thread?
• It is a shared memory task that exists in protected memory task.
What is a task?
• In an OS a task is a job.
• A job has its own memory address space.
• This is called protected memory.
• If one Job dies, the other jobs can continue.
• Each job get some resources according to a program called a scheduler.
What is a task scheduler?
• A program that uses a discipline to decide what resources a job gets.
• priority allows for allocation of various CPU time slices.
What is a thread?
• Like a job that does not have protected memory.
• It shares memory resource with other threads in a given task.
Thread Programming models
1. The boss/worker model
2. The peer model
3. A thread pipeline
P PP P P PMicrokernelMicrokernel
Multi-Processor Computing System
Threads InterfaceThreads Interface
Hardware
Operating System
ProcessProcessor ThreadPP
Applications
Computing Elements
Programming paradigms
What is a memory swap?
• When the memory for a task is swapped to disk so that the next task can run.
• Context switching occurs during a memory swap.
• Context switch take time.
What is thrashing?
• When no progress is made because of the overhead in the context switch.
Why don’t threads thrash?
• Threads share memory.
• Threads have a low-overhead context switch.
• Threads can have their own scheduler.
• I can make thousands of threads with little slowdown.
Threads and tasks
thread1thread2
... threadN
thread1thread2threadN
task 1 task2
What is deadlock?
• when threads or tasks make no progress because of an unavailable resource.
A thread pipeline
Resources Files
Databases
Disks
Special Devices
Files
Databases
Disks
Special Devices
Files
Databases
Disks
Special Devices
Stage 1Stage 1 Stage 2Stage 2 Stage 3Stage 3
Program Filter Threads
Input (Stream)
Threads
• Java has built in thread support for Multithreading
• Synchronization
• Thread Scheduling
How to I make a thread?
• subclass the Thread class OR
• Thread t = new Thread(Runnable r);
What is Runnable?
• Its interface that defines a method like this:
public void run();
public interface Runnable {
public void run();
}
Why do I need Runnable?
• The Thread constructor wants an instance of a class that implements Runnable.
• We need this because the thread scheduler needs a call-back method to execute the Thread.
What are the States of A thread?
new Thread
ready!
t.start()
runningt.run()
dead
run method returns
blocked
t.sleep(), t.wait()
t.notify()notifyAll()
26
Thread states
new
runnable non-runnable
dead
wait()sleep()suspend()blocked
notify()sleptresume()unblocked
start()
stop()
When a thread dies…
• The finalizer is invoked just before a thread is garbage collected.
• The Main group is a group of threads.
• The Main group is a child of the SystemThreadGroup.
Ways of Multithreading in Java
• Create a class that extends the Thread class• Create a class that implements the Runnable
interface
1st Method: Extending the Thread class
class MyThread extends Thread{ public void run() { //… } }• Creating thread: MyThread t = new MyThread();• Start Execution: t.start();
2nd method: Threads by implementing Runnable interface
class ClassName implements Runnable{
.....
public void run() {
// thread body of execution
}
}
Make a new thread
• Creating Thread Object:
Thread t = new Thread(
new Runnable(){
public void run(){
} );• t.start();
Manipulation of Current Thread// CurrentThreadDemo.java
class CurrentThreadDemo {
public static void main(String arg[]) {
Thread ct = Thread.currentThread();
ct.setName( "My Thread" );
System.out.println("Current Thread : "+ct);
try {
for(int i=5; i>0; i--) {
System.out.println(" " + i);
Thread.sleep(1000);
}
}
catch(InterruptedException e) {
System.out.println("Interrupted."); }
}
}
Run:
Current Thread : Thread[My Thread,5,main]
5
4
3
2
1
What is a daemon?
• A kind of thread that dies when main dies.
• Non Daemon threads will cause the JVM to keep running!
What is Threadsafe?
• When the resource is synchronized other threads cannot use at the same time.
• It prevents resource contention
• but it can cause deadlock.
For example
Imperion Thread
RunJobs
RunJob Associations
Recommended