Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.

Similar presentations


Presentation on theme: "Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17."— Presentation transcript:

1 Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17

2 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

3 Introduction to

4 Primary Goals of Java Portable Secured Object Oriented

5 Portable – Platform Independent Source Code Java Compiler ByteCode JVM input output

6 Secured ByteCode instructions Download through Network Verify Code Run it Network

7 Object Oriented Programming Encapsulation Inheritance Polymorphism

8 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

9 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

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

11 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.x  0, p.y  0]

12 Plan Terminology (Partial) rules for execution orders Example programs Threads Locks and synchronization Wait sets and notification Conclusion

13 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

14 Main memory write read lock working memory Thread engine asign use store load lock Java Memory Model unlock working memory Thread engine assign use store load unlock lock

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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 threadmain memory use a assign b read b [store a] [write a] read a load a [store b] [write b] yon thread

25 Example: Possible Swap use b assign a load b hitter threadmain memory use a assign b read b [store a] [write a] read a load a [store b] [write b] yon thread b=2 a=2 b=2 a=2b=2

26 Example: Possible Swap use b assign a load b hitter threadmain memory use a assign b read b [store a] [write a] read a load a [store b] [write b] yon thread b=1 a=1 b=1 a=1b=1

27 Example: Possible Swap use b assign a load b hitter threadmain memory use a assign b read b [store a] [write a] read a load a [store b] [write b] yon thread b=2 a=2 a=1 b=1 a=1b=2

28 (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;

29 (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;

30 (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;

31 (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;

32 (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;

33 (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;

34 Threads Created by the built-in class Thread The start method starts a thread

35 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

36 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

37 Uncovered Volatile variables Prescient Store actions Non-atomic treatment of double of long

38 Summary Java provides a powerful low level model of concurrency Hard to understand But portable Next lesson SOS for Java concurrency


Download ppt "Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17."

Similar presentations


Ads by Google