7. the concurrent extension java t of java ε ps lab 김윤경
TRANSCRIPT
7. The concurrent extension JavaT of Javaε
ps lab 김윤경
content
7.1 Static semantics of JavaT
7.2 Transition rules for JavaT
7.2.1 Scheduling of multiple threads7.2.2 Thread methods
7.3 Thread invariants Example7.3.1
7.1 Static semantics of JavaT
7.1.1 Vocabulary of JavaT
cont : Thread -> (Frame*, Frame)
Fig. 7.1 Syntax of JavaT
Stm := …| synchronized( Exp ) Stm
must be a reference type
monitors : a mechanism for allowing only one thread at a time to execute a region of code protected by the monitor.
locks : When a synchronized statement is processed, the executing thread must grab the lock which is associated with the target reference .
sync: Thread ->Ref*locks : Ref ->NatwaitSet : Ref -> Powerset(Thread)exec : Thread -> ThreadStatedata ThreadState = NotStarted | Active | Synchronizing |
Waiting | Notified | DeadsyncObj : Thread -> RefwaitObj : Thread -> RefInterruptedFlag : Thread -> Bool
7.2 Transition rules for JavaT
7.2.1 Scheduling of multiple threads
execJavaThread= choose q∈dom(exec), runnable(q) if q=thread ∧exec(q) =Active then execJava else if exec(thread)=Active then cont(thread):=(frames,(meth, restbody, pos,locals)) thread :=q
run(q)
runnable(q)= case exec(q) of Active ->True
Synchronizing ->locks(syncObj(q))=0 Notified->locks(waitObj(q))=0
run(q) =switchCont(q)if exec(q) =Synchronizing then
synchronize(q)if exec(q)=Notified then
wakeup(q)
switchCont(q)=let (frames’,(meth’,restbody’,pos’,locals’)) = cont(q)
exec(q):=Active meth:=meth’ restbody:=restbody’ pos:=pos’ locals:=locals’ frames:=frames’
synchronize(q)=sync(q):=[syncObj(q)]·sync(q)locks(syncObj(q)):=1
wakeup(q)=locks(waitObj(q)):=occurrences(waitObj(q),sync(q))
7.2.2 Thread methods
start(ref ) = if exec(ref ) = NotStarted then fail (IllegalThreadStateException) else let q = getField(ref , Thread/ "target" ) meth = lookup(classOf (q),Thread/run())/run() exec(ref ) := Active cont(ref ) := ([ ], (meth, body(meth), firstPos,{( "this”, q)})) yieldUp(Norm)
interrupt(q) = yieldUp(Norm) if exec(q) = Waiting ∧ ¬ classInitialization(q) then let (frames’, (meth’, restbody’, pos’, locals’)) = cont(q) let fail = restbody’[throw new InterruptedException(); /pos’] let ref = waitObj (q) waitSet(ref ) := waitSet(ref ) \ {q}
exec(q) := Notified cont(q) := (frames’, (meth’, fail , pos’, locals’)) interruptedFlag(q) := False else interruptedFlag(q) := True classInitialization(q) = q ∈ ran(initThread) ∨ q ∈ ∪ ran(initWait)
wait(ref ) = if ref ∈ sync(thread) then fail (IllegalMonitorStateException) else let ret = restbody[Norm/up(pos)] waitSet(ref ) := waitSet(ref ) ∪{thread} locks(ref ) := 0 exec(thread) := Waiting waitObj (thread) := ref
cont(thread) := (frames; (meth; ret; up(pos); locals)) yieldUp(Norm)
notify(ref ) =if ref ∈ sync(thread) then
fail (IllegalMonitorStateException)else
yieldUp(Norm)choose q ∈ waitSet(ref )waitSet(ref ) := waitSet(ref ) \ {q}exec(q) := Notified
notifyAll (ref ) = if ref ∈sync(thread) then
fail (IllegalMonitorStateException)else
waitSet(ref ) := 0 yieldUp(Norm)forall q ∈ waitSet(ref )
exec(q) := Notified
7.3 Thread invariantsTheorem 7.3.1(thread) classOf (thread)≤h Thread.(exec1) dom(exec) is exactly the set of all threads.(exec2) If exec(q) =NotStarted, then sync(q) = [ ].(sync1) If ref ∈ sync(q), then ref ∈ dom(heap).(sync2) If synchronizing(q, ref ), then ref ∈ dom(heap), ref ∈ sync(q) and[ref ] sync(q) = syncFromCont(q).(sync3) If exec(q) ∈{Synchronizing, NotStarted}, thensync(q) = syncFromCont(q).(wait1) If waiting(q, ref ), then q ∈ waitSet(ref ) and ref ∈ sync(q).(wait2) If q ∈ waitSet(ref ), then q is a thread and waiting(q, ref ).(notied) If notified(q, ref ), then q ∈ waitSet(ref ) and ref ∈ sync(q).(dead) If exec(q) = Dead, then the frame stack of q is empty, waitSet(q) = 0 and sync
(q) = [ ].(lock1) If locked(q, ref ), then locks(ref ) = occurrences(ref, sync(q)).(lock2) If locked(q1, ref ) and locked(q2, ref ), then q1 = q2.(lock3) If locks(ref) > 0, then locked(ref ).
Example7.3.1
1. he appends a piece of text to the book;2. he notifies another author (or the publisher);3. he releases the lock of the book;4. he waits until he gets notified himself.
If the book is finished, he notifies another author (or the publisher) and terminates his work.
The publisher waits until the book is finished. He then prints the
book and notifies all waiting authors.