Java Thread and Memory Model By Xijun Zhang #104549.
-
Upload
robyn-wilcox -
Category
Documents
-
view
225 -
download
0
Transcript of Java Thread and Memory Model By Xijun Zhang #104549.
![Page 1: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/1.jpg)
Java Thread and Memory Model
By Xijun Zhang
#104549
![Page 2: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/2.jpg)
Question
• Can this result in i = 0 and j = 0?
x=y=0
x=1 y=1
i=y j=x
Thread 1 Thread 2
![Page 3: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/3.jpg)
Contents
• Thread review
• Threads in Java
• Programmer’s view of Java memory model
![Page 4: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/4.jpg)
Threads• Has an execution state (running, ready, etc.)
• Saves thread context when not running
• Has an execution stack and some per-thread static storage for local variables
• Has access to the memory address space and resources of its process– all threads of a process share this– when one thread alters a (non-private) memory
item, all other threads (of the process) sees that– a file open with one thread, is available to
others
![Page 5: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/5.jpg)
Single Threaded and Multithreaded Process Models
• Thread Control Block contains a register image, thread priority and thread state information
![Page 6: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/6.jpg)
Benefits of Threads vs Processes
• Takes less time to create a new thread than a process
• Less time to terminate a thread than a process
• Less time to switch between two threads within the same process
![Page 7: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/7.jpg)
Benefits of Threads
• Since threads within the same process share memory and files, they can communicate with each other without invoking the kernel
• Therefore necessary to synchronize the activities of various threads so that they do not obtain inconsistent views of the data
![Page 8: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/8.jpg)
Java Thread Support Reside in Three Places
• The java.lang.Thread class
• The java.lang.Object class
• The Java language and virtual machine
![Page 9: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/9.jpg)
Two ways to create threads
• Extending the java.lang.Thread class
• Implementing the java.lang.Runnable interface
![Page 10: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/10.jpg)
Extending the Thread class
• Can build a thread by extending java.lang.Thread class
• You must supply a public void run() method
• Start a thread by invoking the start() method
• When a thread starts, it executes run() method
• When run() finished, the thread is finished/dead
![Page 11: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/11.jpg)
Interface Runnable• Extending Thread means can’t extend anything else
• Instead implement Runnable (Declares an object has a void run() method)
• Creating a new thread by giving is an object of type Runnable
• Constructors:
Thread(Runnable target)
Thread(Runnable target, String name)
![Page 12: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/12.jpg)
Thread States
• Running: The state that all threads aspire to
• Various waiting states: Waiting, Sleeping, Suspended, Blocked
• Ready: Not waiting for anything except the CPU
• Dead: All done
![Page 13: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/13.jpg)
Two approaches to implement Thread schedulers
• Preemptive scheduling (e.g. Solaris)
• Time-sliced or round-robin scheduling
e.g. Macintosh, Windows
• Java Thread is platform dependent
![Page 14: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/14.jpg)
Synchronization
• Threads share the same memory space, i.e. they can share resources
• It is desirable that only one thread at a time has access to a shared resource
• Java use the key word synchronized for critical section
![Page 15: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/15.jpg)
Monitors
• At any given time, no more than one thread can own the monitor and thereby have access to the shared resource
• A monitor thus implements a mutually exclusive locking mechanism
• All Java objects have a monitor, and each object can be used as a mutually exclusive lock, providing the ability to synchronize access to shared resources
![Page 16: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/16.jpg)
Synchronized Methods
• A method can be synchronized (add synchronized before the return type)
• Obtains a lock on object referenced by this before starting method ( releases lock when method completes)
• A static synchronized method (locks the class object)
![Page 17: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/17.jpg)
Synchronized statement
• synchronized (obj) {block}
• Obtains a lock on obj before executing block
• Releases lock once block completes
• Provides finer grain of control
• Allows you to lock arguments to a method
![Page 18: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/18.jpg)
Using wait
• a.wait()
--gives up locks on a
--adds thread to wait set for a
--suspends thread
• a.wait(int m)
--limits suspension to m milliseconds
![Page 19: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/19.jpg)
Using notify
• a.notify() resumes one thread from a’s waiting list and remove it from wait set, no control over which thread
• a.notifyAll() resumes one thread on a’s
waiting list
• resumed task must reacquire lock before continuing
![Page 20: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/20.jpg)
Synchronization
• Atomicity
--locking to obtain mutual exclusion
--Atomic read/write granularity• Visibility
--ensuring that changes in object fields on one object are seen in another thread
• Ordering
--ensuring that you aren’t surprised by the order in which statements are executed
![Page 21: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/21.jpg)
Question
• Can this result in i=0 and j=0?
x=y=0
x=1 y=1
i=y j=x
Thread 1 Thread 2
![Page 22: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/22.jpg)
Answer: Yes!
x=y=0
x=1 y=1
i=y j=x
Thread 1 Thread 2
• How can i=0 and j=0?
![Page 23: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/23.jpg)
Working Memory v.s. Main Memory
• Every thread has a working memory in which it keeps its own working copy of variables that it must use or assign. As the thread executes a program, it operates on these working copies.
• The main memory contains the master copy of every variable.
![Page 24: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/24.jpg)
Low level actionsthread
use assign
Working memory(register, cache)
store load
write read
main memory
![Page 25: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/25.jpg)
How can this happen?
• Compiler can reorder statement or keep values in registers
• Processor can reorder them
• On multiprocessor, values not synchronized in global memory
• Must use synchronization to enforce visibility and ordering as well as mutual exclusion
![Page 26: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/26.jpg)
Synchronization Action//block until obtain lock
synchronized (anObject) {
//get main memory value of field1 and field2
int x = anObject.field1;
int y = anObject.field2;
anObject.field3 = x + y;
//commit value of field3 to main memory
}
// release lock
moreCode();
![Page 27: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/27.jpg)
When are actions visible to other thread?
Thread 1
x=1
unlock M
Thread 2 lock M
i=x
![Page 28: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/28.jpg)
What does volatile mean?
• C/C++ spec
--There is no implementation independent meaning of volatile
• Situation a little better with Java Technology
--volatile reads/writes guaranteed to go directly to main memory
e.g. can’t be cached in registers or local memory
![Page 29: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/29.jpg)
Using volatile• Volatile used to guarantee visibility of writes
--stop() must be declared volatile
--Otherwise, compiler could keep in register
class Animator implements Runnable {
private volatile boolean stop = false;
public void stop() { stop = true;}
public void run() {
while (!stop) oneStep();
}
private void oneStep() {/*……*/}
}
![Page 30: Java Thread and Memory Model By Xijun Zhang #104549.](https://reader036.fdocuments.in/reader036/viewer/2022062423/5697bf701a28abf838c7dc50/html5/thumbnails/30.jpg)
Some problems of current Java Memory Model
• Some JVMs do not implement volatile correctly
• There are some corner cases
• Experts are trying to fix the current JMM