Presentation is loading. Please wait.

Presentation is loading. Please wait.

7. The concurrent extension Java T of Java ε ps lab 김윤경.

Similar presentations


Presentation on theme: "7. The concurrent extension Java T of Java ε ps lab 김윤경."— Presentation transcript:

1 7. The concurrent extension Java T of Java ε ps lab 김윤경

2 content 7.1 Static semantics of Java T 7.2 Transition rules for Java T 7.2.1 Scheduling of multiple threads 7.2.2 Thread methods 7.3 Thread invariants Example7.3.1

3 7.1 Static semantics of Java T 7.1.1 Vocabulary of Java T cont : Thread -> (Frame*, Frame) Fig. 7.1 Syntax of Java T Stm := …| synchronized( Exp ) Stm must be a reference type

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

5 sync: Thread ->Ref* locks : Ref ->Nat waitSet : Ref -> Powerset(Thread) exec : Thread -> ThreadState data ThreadState = NotStarted | Active | Synchronizing | Waiting | Notified | Dead syncObj : Thread -> Ref waitObj : Thread -> Ref InterruptedFlag : Thread -> Bool

6 7.2 Transition rules for Java T

7 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

8 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’

9 synchronize(q)= sync(q):=[syncObj(q)]·sync(q) locks(syncObj(q)):=1 wakeup(q)= locks(waitObj(q)):=occurrences(waitObj(q),sync(q))

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

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

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

13 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

14 7.3 Thread invariants Theorem 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}, then sync(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 ).

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


Download ppt "7. The concurrent extension Java T of Java ε ps lab 김윤경."

Similar presentations


Ads by Google