30
Java Thread and Memory Model By Xijun Zhang #104549

Java Thread and Memory Model By Xijun Zhang #104549

Embed Size (px)

Citation preview

Page 1: Java Thread and Memory Model By Xijun Zhang #104549

Java Thread and Memory Model

By Xijun Zhang

#104549

Page 2: Java Thread and Memory Model By Xijun Zhang #104549

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

Contents

• Thread review

• Threads in Java

• Programmer’s view of Java memory model

Page 4: Java Thread and Memory Model By Xijun Zhang #104549

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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