1 / 30

Java Thread and Memory Model

Java Thread and Memory Model. By Xijun Zhang #104549. Question. Can this result in i = 0 and j = 0?. Contents. Thread review Threads in Java Programmer’s view of Java memory model. Threads. Has an execution state (running, ready, etc.) Saves thread context when not running

betty_james
Download Presentation

Java Thread and Memory Model

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Thread and Memory Model By Xijun Zhang #104549

  2. Question • Can this result in i = 0 and j = 0?

  3. Contents • Thread review • Threads in Java • Programmer’s view of Java memory model

  4. 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

  5. Single Threaded and Multithreaded Process Models • Thread Control Block contains a register image, thread priority and thread state information

  6. 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

  7. 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

  8. Java Thread Support Reside in Three Places • The java.lang.Thread class • The java.lang.Object class • The Java language and virtual machine

  9. Two ways to create threads • Extending the java.lang.Thread class • Implementing the java.lang.Runnable interface

  10. 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

  11. 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)

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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)

  17. 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

  18. 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

  19. 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

  20. 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

  21. Question • Can this result in i=0 and j=0?

  22. Answer: Yes! • How can i=0 and j=0?

  23. 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.

  24. Low level actions

  25. 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

  26. 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();

  27. When are actions visible to other thread?

  28. 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

  29. 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() {/*……*/} }

  30. 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

More Related