Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification...
-
date post
20-Dec-2015 -
Category
Documents
-
view
235 -
download
2
Transcript of Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification...
![Page 1: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/1.jpg)
Programming Language SemanticsJava Threads and LocksInformal Introduction
The Java Specification Language
Chapter 17
![Page 2: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/2.jpg)
Motivation• Show that the techniques presented in the course can
handle real programming language
Java Language Features
o Higher order types
o Dynamic memory allocation
o Pointers
o Procedures and recursion
o Parameter passing
o Concurrency
![Page 3: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/3.jpg)
Introduction to
![Page 4: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/4.jpg)
Primary Goals of Java
• Portable
• Secured
• Object Oriented
![Page 5: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/5.jpg)
Portable – Platform Independent
Source Code
Java Compiler
ByteCode
JVMinput output
![Page 6: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/6.jpg)
Secured
• ByteCode instructions• Download through
Network• Verify Code• Run it
Network
![Page 7: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/7.jpg)
Object Oriented Programming
• Encapsulation
• Inheritance
• Polymorphism
![Page 8: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/8.jpg)
C vs. Java
• Macros• Low-level memory
management– Pointer arithmetic p++– Casts – Free– Unsafe arrays– Stack allocation– Big and small L-values
• Object Oriented• Portable• Well defined semantics• Dynamic class loading• References instead of
arbitrary pointers• Heap only• No free (Garbage
collection)• Exceptions
![Page 9: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/9.jpg)
Java Concurrency
• JVM supports threads of execution– Many threads can be executed “simultaneously”– Communicate via shared memory– Synchronize activities using locks
• Standard methods for efficient transfer of control– wait– notify– …
• Special rules about allowed orders of executions
![Page 10: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/10.jpg)
Rules on orders of executions
• Proper use of synchronization allow reliable transmission of values
• Used values are not corrupted– Written by threads
• Provides freedom to the implementation to update memory in an efficient (but surprising) way– (in an absence of synchronization)
![Page 11: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/11.jpg)
Java code fragment
synchronized(p) { p.y = 2; } a = p.x; b = p.y; c = p.y;
synchronized(p) { p.y = 3; p.y = 100; } p.x = 1;
[p.x0, p.y0]
![Page 12: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/12.jpg)
Plan
• Terminology
• (Partial) rules for execution orders
• Example programs
• Threads
• Locks and synchronization
• Wait sets and notification
• Conclusion
![Page 13: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/13.jpg)
Terminology
• Variable any location in the program with an L-value
• Main memory shared by all threads• Working memory local to every thread• Data exchange is asynchronous • thread actions
– use, assign, load, store, lock, unlock
• Main memory actions– read, write, lock, unlock
![Page 14: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/14.jpg)
Main memorywrite
read
lock
workingmemory
Threadengine
asign
use
store
load
lock
Java Memory Model
unlock
unlock
workingmemory
Threadengine
assign
use
store
load
unlock
lock
![Page 15: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/15.jpg)
Action Summary
• use (thread) transfer content of variables to thread’s execution engine
• assign (thread) transfer content of variables into working copy
• read (main memory) transmit content of master
• load (thread) put the transmitted value into a working copy
• store (thread) transmit the working copy to main memory
• write (main memory) puts the transmitted value into main memory
• lock (thread and main memory) a thread acquires a lock
• unlock (thread and main memory) a thread releases the lock
![Page 16: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/16.jpg)
Execution Order
• Actions performed on any thread are totally ordered
• Actions performed by main memory on the same variable are totally ordered
• Actions performed by memory on the same lock are totally ordered
• No action can follow itself
![Page 17: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/17.jpg)
Memory-Thread Interactions
• lock/unlock operations are performed jointly• thread load is uniquely paired with memory read• thread store is uniquely paired with memory write• Other rules constrain the order of actions
– Affect the semantics
– Programmers need to understand in order to write correct and portable programs
– Many possible implementations
![Page 18: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/18.jpg)
Variable Rules
• T thread• V variable• A use or assign by T on V is permitted only when
dictated by the bytecode of T– Executed in order specified by the thread
• A store by T on V must intervene between assign by T on V and subsequent load by T on V
• An assign by T on V must intervene between load/store by T on V and subsequent store by T on V
![Page 19: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/19.jpg)
Variable Rules (cont)
• T thread
• V variable
• After T is created it must perform assign/load on V before use/store on V
• After V is created, every thread must perform assign/load before use/store on V
![Page 20: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/20.jpg)
Variable Rules (main memory)
• Every load performed by T on its working copy of V there exists a corresponding read by main memory from the master copy of V
• Every store performed by T of its working copy of V there exists a corresponding write by main memory of V into the master copy
![Page 21: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/21.jpg)
Variable Rules (main memory)
• A be a load/store performed by T – P be the corresponding read/write by main
memory
• B be a load/store performed by T – Q be the corresponding read/write by main
memory
• If A precedes B then P precedes Q
![Page 22: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/22.jpg)
Locks Rules
• T thread• L lock• lock by T on L may occur only if every thread S
other T the number of preceding unlocks by S on L = the number of preceding locks on L by S
• unlock by T may occur only if the number preceding unlocks by T < the number of preceding locks by T
• lock/unlock performed in sequential order
![Page 23: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/23.jpg)
Interactions of Locks and Variables
• Between assign by T of V and a subsequent unlock by T on L– store of V by T must intervene
– The corresponding write must precede the unlock
• Between lock by T on L and a subsequent use/store by T of V– assign/load of V by T must intervene
– For load, the corresponding read must follow the lock
![Page 24: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/24.jpg)
Example: Possible Swap
class sample {
int a = 1, b = 2;
void hither(){
a = b;
}
void yon(){
b = a ;
}
use b
assign a
load b
hitter thread main memory
use a
assign b
read b
[store a]
[write a]
read a
load a
[store b]
[write b]
yon thread
![Page 25: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/25.jpg)
Example: Possible Swap
use b
assign a
load b
hitter thread main memory
use a
assign b
read b
[store a]
[write a]
read a
load a
[store b]
[write b]
yon thread
b=2
b=2
a=2
a=2
a=2
a=2
a=2
b=2
b=2
b=2
a=2b=2
![Page 26: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/26.jpg)
Example: Possible Swap
use b
assign a
load b
hitter thread main memory
use a
assign b
read b
[store a]
[write a]
read a
load a
[store b]
[write b]
yon thread
b=1
b=1
a=1
a=1
a=1
a=1
a=1
b=1
b=1
b=1
a=1b=1
![Page 27: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/27.jpg)
Example: Possible Swap
use b
assign a
load b
hitter thread main memory
use a
assign b
read b
[store a]
[write a]
read a
load a
[store b]
[write b]
yon thread
b=2
b=2
a=2
a=2
a=2
a=1
a=1
b=1
b=1
b=1
a=1b=2
![Page 28: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/28.jpg)
(lock,1,o)
(assign,1,y,2)
(store,1,y,2)
(unlock,1,o)
(load,1,x,1)
(use,1,x,1)
(use,1,y,2)
(load,1,y,3)
(use,1,y,3)
(write,1,y,2)
(write,2,y,3)
(read,1,y,3)
(write,2,y,100)
(lock,2,o)
(assign,2,y,3)
(store,2,y,3)
(assign,2,y,100)
(store,2,y,100)
(unlock,2,o)
(assign,2,x,1)
(store,2,x,1)(write,2,x,1)
(read,1,x,1)
(1) synchronized(p) { p.y = 2; } a = p.x; b = p.y; c = p.y;(2) synchronized(p) { p.y = 3; p.y = 100; } p.x = 1;
![Page 29: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/29.jpg)
(lock,1,o)
(assign,1,y,2)
(store,1,y,2)
(unlock,1,o)
(load,1,x,1)
(use,1,x,1)
(use,1,y,2)
(load,1,y,3)
(use,1,y,3)
(write,1,y,2)
(write,2,y,3)
(read,1,y,3)
(write,2,y,100)
(lock,2,o)
(assign,2,y,3)
(store,2,y,3)
(assign,2,y,100)
(store,2,y,100)
(unlock,2,o)
(assign,2,x,1)
(store,2,x,1)(write,2,x,1)
(read,1,x,1)
(1) synchronized(p) { p.y = 2; } a = p.x; b = p.y; c = p.y;(2) synchronized(p) { p.y = 3; p.y = 100; } p.x = 1;
![Page 30: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/30.jpg)
(lock,1,o)
(assign,1,y,2)
(store,1,y,2)
(unlock,1,o)
(load,1,x,1)
(use,1,x,1)
(use,1,y,2)
(load,1,y,3)
(use,1,y,3)
(write,1,y,2)
(write,2,y,3)
(read,1,y,3)
(write,2,y,100)
(lock,2,o)
(assign,2,y,3)
(store,2,y,3)
(assign,2,y,100)
(store,2,y,100)
(unlock,2,o)
(assign,2,x,1)
(store,2,x,1)(write,2,x,1)
(read,1,x,1)
(1) synchronized(p) { p.y = 2; } a = p.x; b = p.y; c = p.y;(2) synchronized(p) { p.y = 3; p.y = 100; } p.x = 1;
![Page 31: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/31.jpg)
(lock,1,o)
(assign,1,y,2)
(store,1,y,2)
(unlock,1,o)
(load,1,x,1)
(use,1,x,1)
(use,1,y,2)
(load,1,y,3)
(use,1,y,3)
(write,1,y,2)
(write,2,y,3)
(read,1,y,3)
(write,2,y,100)
(lock,2,o)
(assign,2,y,3)
(store,2,y,3)
(assign,2,y,100)
(store,2,y,100)
(unlock,2,o)
(assign,2,x,1)
(store,2,x,1)(write,2,x,1)
(read,1,x,1)
(1) synchronized(p) { p.y = 2; } a = p.x; b = p.y; c = p.y;(2) synchronized(p) { p.y = 3; p.y = 100; } p.x = 1;
![Page 32: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/32.jpg)
(lock,1,o)
(assign,1,y,2)
(store,1,y,2)
(unlock,1,o)
(load,1,x,1)
(use,1,x,1)
(use,1,y,2)
(load,1,y,3)
(use,1,y,3)
(write,1,y,2)
(write,2,y,3)
(read,1,y,3)
(write,2,y,100)
(lock,2,o)
(assign,2,y,3)
(store,2,y,3)
(assign,2,y,100)
(store,2,y,100)
(unlock,2,o)
(assign,2,x,1)
(store,2,x,1)(write,2,x,1)
(read,1,x,1)
(1) synchronized(p) { p.y = 2; } a = p.x; b = p.y; c = p.y;(2) synchronized(p) { p.y = 3; p.y = 100; } p.x = 1;
![Page 33: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/33.jpg)
(lock,1,o)
(assign,1,y,2)
(store,1,y,2)
(unlock,1,o)
(load,1,x,1)
(use,1,x,1)
(use,1,y,2)
(load,1,y,3)
(use,1,y,3)
(write,1,y,2)
(write,2,y,3)
(read,1,y,3)
(write,2,y,100)
(lock,2,o)
(assign,2,y,3)
(store,2,y,3)
(assign,2,y,100)
(store,2,y,100)
(unlock,2,o)
(assign,2,x,1)
(store,2,x,1)(write,2,x,1)
(read,1,x,1)
(1) synchronized(p) { p.y = 2; } a = p.x; b = p.y; c = p.y;(2) synchronized(p) { p.y = 3; p.y = 100; } p.x = 1;
![Page 34: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/34.jpg)
Threads
• Created by the built-in class Thread
• The start method starts a thread
![Page 35: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/35.jpg)
Locks and Synchronization
• Every object is a lock• The synchronized statement computes a
reference to an object and issues lock L • After lock L complete the body is executed• When the body is completed unlock L is
executed • A synchronized method is a sugar for
synchronizing the method body
![Page 36: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/36.jpg)
Wait sets and notification
• Every object also has wait set of threads• Used by methods wait, notify, notifyAll
– wait releases the lock and adds thread to wait set
– The thread waits until• notify schedules and remove one thread from wait
set• notifyAll occurs• Specified timeout
![Page 37: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/37.jpg)
Uncovered
• Volatile variables
• Prescient Store actions
• Non-atomic treatment of double of long
![Page 38: Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d4c5503460f94a2a452/html5/thumbnails/38.jpg)
Summary
• Java provides a powerful low level model of concurrency
• Hard to understand
• But portable
• Next lesson SOS for Java concurrency