Download The Life of A Thread

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Object-oriented programming wikipedia , lookup

Name mangling wikipedia , lookup

Library (computing) wikipedia , lookup

C Sharp syntax wikipedia , lookup

Scala (programming language) wikipedia , lookup

Stream processing wikipedia , lookup

Parallel computing wikipedia , lookup

Java (programming language) wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Scheduling (computing) wikipedia , lookup

Java performance wikipedia , lookup

Monitor (synchronization) wikipedia , lookup

Thread (computing) wikipedia , lookup

Transcript
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
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: 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
Multithreading: General concepts
Multithreading:
• 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: General concepts
One-process-one-thread model:
Process
Control
Block
User
Address
Space
User
Stack
Kernel
Stack
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..)
Multithreading: General concepts
One process multiple threads model:
Process
Control
Block
Process control block:
1. Process identification
2. Processor state information
3. Process control information
User
Address
Space
Thread
Control Block
User
Stack
Kernel
Stack
Thread
Control Block
User
Stack
Kernel
Stack
Thread control block:
1. Thread register image
2. Thread priority
3. Thread state information
Multithreading: General concepts
Time schedule of execution (i.e. a Java program with two threads) :
•One process one thread model :
thread1 is blocked
Time
thread1 runs
thread1 is dead
thread1 continues
thread2 begin to run
•One process multiple threads model:
thread1 is blocked
thread1:
thread1 runs
thread2:
thread1 continues
CPU Time needed to
Execute the program
Blocked or dead
Time
thread2 into running state
thread1 running
thread2 running
Multithreading in Java: Program
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.
Runnable
Born
1
3
Ready
2
Dead
Blocked
Running
switch using an explicit command
4
Waiting
Sleeping
5
Blocking I/O
switch when some conditions meet
1: start
2: yield
3: notify or
notifyAll
4: wait
5: sleep
Multithreading in Java: Program
The methods used with threads
• Start
• Run
• Wait
: This method launches a thread to enter the ready state and
begins to invoke related run method.
: The code inside executes the “real work” of a thread.
: When a running method calls wait method the thread
enters a waiting state for particular object in which the thread
was running.
• Notify
• Sleep
• Yield
: 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.
:This gives lower-priority threads a chance to run. A
sleeping thread becomes ready after the designated sleep timeout.
: A thread can call yield to give other threads a chance to
execute.
Multithreading in Java: Program
Edit a subclass into a thread: approach 1
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 2
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.,
Class TimePrinter extends JApplet implements Runnable {
…
Thread thread1 = new Thread( new TimePrinter (…));
thread1.start();
}
Multithreading in Java: Program
Synchronizing 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
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;
}
}
Multithreading in Java: multithreading models
Multithreading implementation
• The steps of editing, compiling and interpreting multithreads
can be thought as Java application.
• Two main categories of multiple threads implementation :
• User level threads (ULT)
• Kernel level threads (KLT)
• The mapping of multithreads from the user level to the kernel
level can be divided as three models:
• Many-to-One (multiple ULTs to one KLT)
• One-to-One (one ULT to one KLT)
• Many-to-Many (multiple ULTs to multiple KLTs)
Multithreading in Java: multithreading models
Many-to-One model: Green threads in Solaris
LWP
Kernel
Java application
(JVM)
User space
Kernel space
(Green threads)
CPU
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).
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”.
•Disadvantages :
•One thread is blocked, all threads are blocked
•Can not run on multiprocessors in parallel
Multithreading in Java: multithreading models
One-to-One model: in Windows NT
LWP
LWP
Java Application
(JVM)
User space
Kernel
Kernel space
CPU
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.
Multithreading in Java: multithreading models
Many-to-Many model: Naive threads in Solaris
LWP
LWP
CPU
Kernel
Java Application
User space
(Native threads)
Kernel space
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
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.