programming language semantics java threads and locks informal introduction the java specification...

Post on 20-Dec-2015

243 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Programming Language SemanticsJava Threads and LocksInformal Introduction

The Java Specification Language

Chapter 17

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

Introduction to

Primary Goals of Java

• Portable

• Secured

• Object Oriented

Portable – Platform Independent

Source Code

Java Compiler

ByteCode

JVMinput output

Secured

• ByteCode instructions• Download through

Network• Verify Code• Run it

Network

Object Oriented Programming

• Encapsulation

• Inheritance

• Polymorphism

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

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

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)

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]

Plan

• Terminology

• (Partial) rules for execution orders

• Example programs

• Threads

• Locks and synchronization

• Wait sets and notification

• Conclusion

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

Main memorywrite

read

lock

workingmemory

Threadengine

asign

use

store

load

lock

Java Memory Model

unlock

unlock

workingmemory

Threadengine

assign

use

store

load

unlock

lock

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Threads

• Created by the built-in class Thread

• The start method starts a thread

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

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

Uncovered

• Volatile variables

• Prescient Store actions

• Non-atomic treatment of double of long

Summary

• Java provides a powerful low level model of concurrency

• Hard to understand

• But portable

• Next lesson SOS for Java concurrency

top related