threads concurrency in java. what is mult-tasking? doing more than one task

38
Threads Concurrency in Java

Upload: laura-taylor

Post on 02-Jan-2016

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

Threads

Concurrency in Java

Page 2: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

What is mult-tasking?

• Doing more than one task.

Page 3: 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

Page 4: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

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

Page 5: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

Ideal speedup

0

2

4

6

8

10

12

1 2 3 4 5 6 7 8 9 10

num ofCPUs

IdealSpeedup

typicalspeedup

Page 6: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

What is multi-threading?

• When you have multiple threads.

Page 7: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

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

Page 8: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

What is a thread?

• It is a shared memory task that exists in protected memory task.

Page 9: Threads Concurrency in Java. What is mult-tasking? Doing more than one 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.

Page 10: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

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.

Page 11: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

What is a thread?

• Like a job that does not have protected memory.

• It shares memory resource with other threads in a given task.

Page 12: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

Thread Programming models

1. The boss/worker model

2. The peer model

3. A thread pipeline

Page 13: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

P PP P P PMicrokernelMicrokernel

Multi-Processor Computing System

Threads InterfaceThreads Interface

Hardware

Operating System

ProcessProcessor ThreadPP

Applications

Computing Elements

Programming paradigms

Page 14: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

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.

Page 15: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

What is thrashing?

• When no progress is made because of the overhead in the context switch.

Page 16: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

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.

Page 17: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

Threads and tasks

thread1thread2

... threadN

thread1thread2threadN

task 1 task2

Page 18: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

What is deadlock?

• when threads or tasks make no progress because of an unavailable resource.

Page 19: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

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)

Page 20: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

Threads

• Java has built in thread support for Multithreading

• Synchronization

• Thread Scheduling

Page 21: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

How to I make a thread?

• subclass the Thread class OR

• Thread t = new Thread(Runnable r);

Page 22: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

What is Runnable?

• Its interface that defines a method like this:

public void run();

public interface Runnable {

public void run();

}

Page 23: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

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.

Page 24: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

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()

Page 25: Threads Concurrency in Java. What is mult-tasking? Doing more than one task
Page 26: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

26

Thread states

new

runnable non-runnable

dead

wait()sleep()suspend()blocked

notify()sleptresume()unblocked

start()

stop()

Page 27: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

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.

Page 28: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

Ways of Multithreading in Java

• Create a class that extends the Thread class• Create a class that implements the Runnable

interface

Page 29: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

1st Method: Extending the Thread class

class MyThread extends Thread{ public void run() { //… } }• Creating thread: MyThread t = new MyThread();• Start Execution: t.start();

Page 30: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

2nd method: Threads by implementing Runnable interface

class ClassName implements Runnable{

.....

public void run() {

// thread body of execution

}

}

Page 31: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

Make a new thread

• Creating Thread Object:

Thread t = new Thread(

new Runnable(){

public void run(){

} );• t.start();

Page 32: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

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

Page 33: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

What is a daemon?

• A kind of thread that dies when main dies.

• Non Daemon threads will cause the JVM to keep running!

Page 34: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

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.

Page 35: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

For example

Page 36: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

Imperion Thread

Page 37: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

RunJobs

Page 38: Threads Concurrency in Java. What is mult-tasking? Doing more than one task

RunJob Associations