multithreading in java

20
Multithreading in Java Project of COCS 513 By Wei Li December, 2000

Upload: marshall-maddox

Post on 31-Dec-2015

14 views

Category:

Documents


0 download

DESCRIPTION

Multithreading in Java. Project of COCS 513 By Wei Li. December, 2000. Introduction:. Multithreading means the concurrent operation of more than one path of execution within a computer. Java is unique among popular general-purpose - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Multithreading in Java

Multithreading in Java

Project of COCS 513 By

Wei Li

December, 2000

Page 2: Multithreading in Java

Introduction:

• Java is unique among popular general-purpose programming languages in that it makes concurrency primitives available to the application programmer.

• Java multithreading refers to the ability of Java to support multiple threads of execution with in a single program.

• Multithreading means the concurrent operation of more than one path of execution within a computer.

Page 3: Multithreading in Java

Multithreading: General concepts

Process:

• a program in execution • associated with data and execution context• an entity that can be assigned to CPU and executed on it •controlled at kernel level Thread:• a dispatchable unit of work within a process• can execute sequentially and can be interrupted• controlled at kernel level or user level

Page 4: Multithreading in Java

Multithreading: General concepts

• Definition: A technique in which threads of a process can run simultaneously

• Benefits:•Give the programmer a greater control over the timing of

application related events• All threads within the same process can share the same data and resources and share a part of the process’s execution context•Save execution time comparing with multi-processes

Multithreading:

Page 5: Multithreading in Java

Multithreading: General conceptsOne-process-one-thread model:

ProcessControlBlock

ProcessControlBlock

UserAddressSpace

UserAddressSpace

UserStack

UserStack

KernelStack

KernelStack

Process control block:

• Process identification• Processor state information( i.e., register information and stack pointer)• Process control information(i.e., scheduling and state information,

data structuring, process privileges, memory management, etc..)

Page 6: Multithreading in Java

Multithreading: General conceptsOne process multiple threads model:

ProcessControlBlock

ProcessControlBlock

UserAddressSpace

UserAddressSpace

UserStack

KernelStack

Thread Control Block

Thread Control Block

UserStack

KernelStack

Process control block: Thread control block:

1. Process identification 1. Thread register image

2. Processor state information 2. Thread priority

3. Process control information 3. Thread state information

Page 7: Multithreading in Java

Multithreading: General concepts

•One process one thread model :Time schedule of execution (i.e. a Java program with two threads) :

thread1 runs thread1 continues

•One process multiple threads model:

thread2 begin to run

thread1 is blocked

Time

thread1 runs thread1 continues

thread1 is blocked

thread2 into running state

Blocked or dead

thread1 running

thread2 runningTime

thread1:

thread2:

CPU Time needed to Execute the program

thread1 is dead

Page 8: Multithreading in Java

Multithreading in Java: Program

ReadyReady

RunningRunning

SleepingSleepingWaitingWaiting

Blocking I/OBlocking I/O

Runnable

DeadDead

BlockedBornBorn

switch using an explicit command switch when some conditions meet

Thread states: At any time, a thread is in one of several states. Some switches can be decided at editing program, others occur when some conditions meet at Java run-time environment. The conditions can be set by programmers or the operating system.

11

2

3

45

1: start 2: yield 3: notify or notifyAll 4: wait 5: sleep

Page 9: Multithreading in Java

The methods used with threads

• Start: This method launches a thread to enter the ready state and begins to invoke related run method.

• Run: The code inside executes the “real work” of a thread.

• Wait: When a running method calls wait method the thread

enters a waiting state for particular object in which the thread was running.

• Notify: A thread in the waiting state for a particular object becomes ready on a call to notify issued by another thread associated with that object.

• Sleep:This gives lower-priority threads a chance to run. A sleeping thread becomes ready after the designated sleep timeout.

• Yield: A thread can call yield to give other threads a chance to execute.

Multithreading in Java: Program

Page 10: Multithreading in Java

Multithreading in Java: Program

A subclass by inheriting Java class Thread (package java.lang) and overriding its run method to realize multithreading, i.e.,

Class TimePrinter extends Thread {…}

Edit a subclass into a thread: approach 1

Edit a subclass into a thread: approach 2

Class TimePrinter extends JApplet implements Runnable { … Thread thread1 = new Thread( new TimePrinter (…)); thread1.start();}

An subclass by implementing Java interface Runnable and automatically take run method. It has to be run from within an instance of Thread class, i.e.,

Page 11: Multithreading in Java

Multithreading in Java: ProgramSynchronizing Threads:

• Prevent collisions in which more than one thread attempts to modify the same object at the same time.

• Java Object class provides each object a lock. The lock can be manipulated only by JVM (Java Virtual Machine) and allow only one thread at a time to access the object. • Synchronizing in Java program is at object level. For convenience, synchronized keyword can be used as a method modifier. If one thread is locked by calling the synchronized method in an object, the all methods in this object can not be invoked by other threads.

• Use wait and notify (or notifyAll) to realize threads coordinating

Page 12: Multithreading in Java

Multithreading in Java: Program

A example of using “synchronized” keyword:

public class Account{ float amount;

public Account() {…}

Public synchronized void deposit ( float amt){ amount +=amt; }

Public synchronized void withdraw (float amt){ amount -=amt; }}

Page 13: Multithreading in Java

Multithreading in Java: multithreading models

• The steps of editing, compiling and interpreting multithreads can be thought as Java application.

• Many-to-One (multiple ULTs to one KLT)• One-to-One (one ULT to one KLT)• Many-to-Many (multiple ULTs to multiple KLTs)

• Two main categories of multiple threads implementation :

Multithreading implementation

• The mapping of multithreads from the user level to the kernel level can be divided as three models:

• User level threads (ULT) • Kernel level threads (KLT)

Page 14: Multithreading in Java

Multithreading in Java: multithreading models

Many-to-One model: Green threads in Solaris

Java application User space

(JVM) (Green threads)Kernel space

Kernel CPU

LWP

Page 15: Multithreading in Java

Multithreading in Java: multithreading models

Many-to-One: Green threads in Solaris

• Multiple ULTs to one KLT

• Threads library is stored in Java Development Kit (JDK).

•Disadvantages :•One thread is blocked, all threads are blocked•Can not run on multiprocessors in parallel

Thread library is a package of code for user level thread management, i.e. scheduling thread execution and saving thread contexts, etc.. In Solaris threads library is called“green threads”.

Page 16: Multithreading in Java

Multithreading in Java: multithreading models

One-to-One model: in Windows NT

Java Application User space

(JVM)

Kernel spaceKernel CPU

LWP

LWP

Page 17: Multithreading in Java

Multithreading in Java: multithreading models

One-to-One model: in Windows NT

• One ULT to one KLT

• Realized by Windows NT threads package.

• The kernel maintains context information for the process and for individual thread.

• Disadvantage: The time of switching one thread to another thread at kernel level is much longer than at user level.

Page 18: Multithreading in Java

Multithreading in Java: multithreading models

Many-to-Many model: Naive threads in Solaris

Java Application User spaceKernel space

Kernel

CPU

LWP

LWP

(Native threads)

Page 19: Multithreading in Java

Multithreading in Java: multithreading models

Many-to-Many model: Native threads in Solaris

• Two level model or combined model of ULT and KLT

• In Solaris operating system, native threads library can be invoked by setting THREADS_FLAG in JDK to native environment.

• A user level threads library (Native threads), provided by JDK, can schedule user-level threads above kernel-level threads.

• The kernel only need to manage the threads that are currently active.

• Solve the problems in two models above

Page 20: Multithreading in Java

Multithreading in Java: Conclusion

• Multithreading is one of the most important features in Java.

• Using multithreading, the execution of different threads in a process can be realized at the same time.

• In Java programming, multithreading means the application of class Thread.

• In implementation, multithreading means some models of mapping threads from the user level to the kernel level. The mapping models are operating system dependent.