Download presentation
Presentation is loading. Please wait.
Published byLaura Taylor Modified over 9 years ago
1
Threads Concurrency in Java
2
What is mult-tasking? Doing more than one task.
3
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
4
ä 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
5
Ideal speedup
6
What is multi-threading? When you have multiple threads.
7
Single and Multithreaded Processes Single-threaded Process Single instruction stream Multiple instruction stream Multiplethreaded Process Threads of Execution Common Address Space threads are light-weight processes within a process
8
What is a thread? It is a shared memory task that exists in protected memory task.
9
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.
10
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.
11
What is a thread? Like a job that does not have protected memory. It shares memory resource with other threads in a given task.
12
Thread Programming models 1. The boss/worker model 2. The peer model 3. A thread pipeline
13
PPPPPP Microkernel Multi-Processor Computing System Threads Interface Hardware Operating System Process Processor Thread P P Applications Computing Elements Programming paradigms
14
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.
15
What is thrashing? When no progress is made because of the overhead in the context switch.
16
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.
17
Threads and tasks thread1 thread2... threadN thread1 thread2 threadN task 1 task2
18
What is deadlock? when threads or tasks make no progress because of an unavailable resource.
19
A thread pipeline Resources Files Databases Disks Special Devices Files Databases Disks Special Devices Files Databases Disks Special Devices Stage 1 Stage 2 Stage 3 Program Filter Threads Input (Stream)
20
Threads Java has built in thread support for Multithreading Synchronization Thread Scheduling
21
How to I make a thread? subclass the Thread class OR Thread t = new Thread(Runnable r);
22
What is Runnable? Its interface that defines a method like this: public void run(); public interface Runnable { public void run(); }
23
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.
24
What are the States of A thread? new Thread ready! t.start() running t.run() dead run method returns blocked t.sleep(), t.wait() t.notify() notifyAll()
26
26 Thread states new runnable non-runnable dead wait() sleep() suspend() blocked notify() slept resume() unblocked start() stop()
27
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.
28
Ways of Multithreading in Java Create a class that extends the Thread class Create a class that implements the Runnable interface
29
1st Method: Extending the Thread class class MyThread extends Thread{ public void run() { //… } Creating thread: MyThread t = new MyThread(); Start Execution: t.start();
30
2nd method: Threads by implementing Runnable interface class ClassName implements Runnable{..... public void run() { // thread body of execution }
31
Make a new thread Creating Thread Object: Thread t = new Thread( new Runnable(){ public void run(){ } ); t.start();
32
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
33
What is a daemon? A kind of thread that dies when main dies. Non Daemon threads will cause the JVM to keep running!
34
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.
35
For example
36
Imperion Thread
37
RunJobs
38
RunJob Associations
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.