Chapter 3 Concurrent execution &RQFHSWVSURFHVVHVFRQFXUUHQWH[HFXWLRQ Concurrent Execution DQGLQWHUOHDYLQJ SURFHVVLQWHUDFWLRQ 0RGHOV SDUDOOHOFRPSRVLWLRQRIDV\QFKURQRXVSURFHVVHV LQWHUOHDYLQJ LQWHUDFWLRQVKDUHGDFWLRQV SURFHVVODEHOLQJDQGDFWLRQUHODEHOLQJDQGKLGLQJ VWUXFWXUHGLDJUDPV 3UDFWLFH 0XOWLWKUHDGHG-DYDSURJUDPV Concurrency: concurrent execution 1 Concurrency: concurrent execution ©Magee/Kramer Definitions 3.1 Modeling Concurrency &RQFXUUHQF\ z /RJLFDOO\ VLPXOWDQHRXV SURFHVVLQJ 'RHV QRW LPSO\ PXOWLSOH SURFHVVLQJ HOHPHQWV 3(V 5HTXLUHV LQWHUOHDYHG H[HFXWLRQ RQ D VLQJOH 3( 2 ©Magee/Kramer +RZVKRXOGZHPRGHOSURFHVVH[HFXWLRQVSHHG" z A B DUELWUDU\ VSHHG ZH DEVWUDFW DZD\ WLPH C +RZGRZHPRGHOFRQFXUUHQF\" 3DUDOOHOLVP z 3K\VLFDOO\ VLPXOWDQHRXV SURFHVVLQJ Time z ,QYROYHV PXOWLSOH 3(V DQGRU DUELWUDU\ UHODWLYH RUGHU RI DFWLRQV IURP GLIIHUHQW SURFHVVHV LQWHUOHDYLQJ EXW SUHVHUYDWLRQ RI HDFK SURFHVV RUGHU LQGHSHQGHQW GHYLFH RSHUDWLRQV :KDWLVWKHUHVXOW" %RWK FRQFXUUHQF\ DQG SDUDOOHOLVP UHTXLUH FRQWUROOHG DFFHVV WR z VKDUHG UHVRXUFHV :H XVH WKH WHUPV SDUDOOHO DQG FRQFXUUHQW LQWHUFKDQJHDEO\ DQG JHQHUDOO\ GR QRW GLVWLQJXLVK EHWZHHQ UHDO DQG SVHXGRFRQFXUUHQW H[HFXWLRQ Concurrency: concurrent execution SURYLGHV D JHQHUDO PRGHO LQGHSHQGHQW RI VFKHGXOLQJ 3 ©Magee/Kramer DV\QFKURQRXV PRGHO RI H[HFXWLRQ Concurrency: concurrent execution 4 ©Magee/Kramer parallel composition - action interleaving parallel composition - action interleaving scratch think ,I3DQG4DUHSURFHVVHVWKHQ3__4UHSUHVHQWVWKH FRQFXUUHQWH[HFXWLRQRI3DQG47KHRSHUDWRU__LV WKHSDUDOOHOFRPSRVLWLRQRSHUDWRU ITCH 0 1 0 VWDWHV think VWDWHV talk scratch CONVERSE_ITCH 3RVVLEOH WUDFHV DV D UHVXOW RI DFWLRQ LQWHUOHDYLQJ 5 0 1 2 3 4 talk IURP ,7&+ IURP &219(56( Concurrency: concurrent execution 5 think parallel composition - algebraic laws 6 ©Magee/Kramer modeling interaction - shared actions ,ISURFHVVHVLQDFRPSRVLWLRQKDYHDFWLRQVLQFRPPRQ WKHVHDFWLRQVDUHVDLGWREHVKDUHG6KDUHGDFWLRQVDUH WKHZD\WKDWSURFHVVLQWHUDFWLRQLVPRGHOHG:KLOH XQVKDUHGDFWLRQVPD\EHDUELWUDULO\LQWHUOHDYHGD VKDUHGDFWLRQPXVWEHH[HFXWHGDWWKHVDPHWLPHE\DOO SURFHVVHVWKDWSDUWLFLSDWHLQWKHVKDUHGDFWLRQ (P||Q) = (Q||P) (P||(Q||R)) = ((P||Q)||R) = (P||Q||R). &ORFNUDGLRH[DPSOH CLOCK = (tick->CLOCK). RADIO = (on->off->RADIO). MAKER = (make->ready->MAKER). USER = (ready->use->USER). ||CLOCK_RADIO = (CLOCK || RADIO). ||MAKER_USER = (MAKER || USER). /76"7UDFHV"1XPEHURIVWDWHV" Concurrency: concurrent execution [ VWDWHV ©Magee/Kramer &RPPXWDWLYH $VVRFLDWLYH 2 scratch ||CONVERSE_ITCH = (ITCH || CONVERSE). Concurrency: concurrent execution 1 scratch ITCH = (scratch->STOP). CONVERSE = (think->talk->STOP). thinkÆtalkÆscratch thinkÆscratchÆtalk scratchÆthinkÆtalk talk CONVERSE MAKER V\QFKURQL]HV ZLWKUSER ZKHQready /76"7UDFHV"1XPEHURIVWDWHV" 7 ©Magee/Kramer Concurrency: concurrent execution 8 ©Magee/Kramer modeling interaction - handshake modeling interaction - multiple processes $KDQGVKDNHLVDQDFWLRQDFNQRZOHGJHGE\DQRWKHU 0XOWLSDUW\V\QFKURQL]DWLRQ MAKE_A = (makeA->ready->used->MAKE_A). MAKE_B = (makeB->ready->used->MAKE_B). ASSEMBLE = (ready->assemble->used->ASSEMBLE). MAKERv2 = (make->ready->used->MAKERv2). USERv2 = (ready->use->used ->USERv2). VWDWHV VWDWHV ||MAKER_USERv2 = (MAKERv2 || USERv2). [ VWDWHV" make 0 ready 1 use 2 ||FACTORY = (MAKE_A || MAKE_B || ASSEMBLE). makeA VWDWHV 3 makeB makeA ready assemble ,QWHUDFWLRQ 0 FRQVWUDLQV 1 2 3 4 5 WKH RYHUDOO EHKDYLRXU used Concurrency: concurrent execution 9 Concurrency: concurrent execution makeB ©Magee/Kramer 10 ©Magee/Kramer used composite processes process labeling $FRPSRVLWHSURFHVVLVDSDUDOOHOFRPSRVLWLRQRISULPLWLYH SURFHVVHV7KHVHFRPSRVLWHSURFHVVHVFDQEHXVHGLQWKH GHILQLWLRQRIIXUWKHUFRPSRVLWLRQV D3SUHIL[HVHDFKDFWLRQODEHOLQWKHDOSKDEHWRI3ZLWKD 7ZRLQVWDQFHVRIDVZLWFKSURFHVV SWITCH = (on->off->SWITCH). ||MAKERS = (MAKE_A || MAKE_B). ||TWO_SWITCH = (a:SWITCH || b:SWITCH). a.on ||FACTORY = (MAKERS || ASSEMBLE). 6XEVWLWXWLQJ WKH GHILQLWLRQ IRU MAKERS LQ FACTORY b:SWITCH 0 DQG DSSO\LQJ WKH FRPPXWDWLYH DQG DVVRFLDWLYH ODZV IRU SDUDOOHO FRPSRVLWLRQ UHVXOWV LQ WKH RULJLQDO GHILQLWLRQ IRU FACTORY 1 a.off LQ WHUPV RI SULPLWLYH SURFHVVHV ||FACTORY = (MAKE_A || MAKE_B || ASSEMBLE). Concurrency: concurrent execution b.on a:SWITCH 11 ©Magee/Kramer 0 1 b.off $QDUUD\RILQVWDQFHVRIWKHVZLWFKSURFHVV ||SWITCHES(N=3) = (forall[i:1..N] s[i]:SWITCH). ||SWITCHES(N=3) = (s[i:1..N]:SWITCH). Concurrency: concurrent execution 12 ©Magee/Kramer process labeling by a set of prefix labels process prefix labels for shared resources a.acquire ^DD[`3UHSODFHVHYHU\DFWLRQODEHOQLQWKH DOSKDEHWRI3ZLWKWKHODEHOVDQ«D[Q)XUWKHU HYHU\WUDQVLWLRQQ!;LQWKHGHILQLWLRQRI3LV UHSODFHGZLWKWKHWUDQVLWLRQV^DQ«D[Q`!; a.use b.acquire a:USER 0 1 2 0 1 a.release 3URFHVVSUHIL[LQJLVXVHIXOIRUPRGHOLQJVKDUHGUHVRXUFHV RESOURCE = (acquire->release->RESOURCE). USER = (acquire->use->release->USER). b.use b:USER 2 b.release +RZ GRHV WKH PRGHO HQVXUH WKDW WKH XVHU WKDW DFTXLUHV WKH UHVRXUFH LV WKH RQH WR UHOHDVH LW" b.acquire a.acquire {a,b}::RESOURCE 0 1 a.acquire b.acquire a.release b.release b.use a.use RESOURCE_SHARE 0 1 2 3 4 ||RESOURCE_SHARE = (a:USER || b:USER || {a,b}::RESOURCE). b.release Concurrency: concurrent execution 13 Concurrency: concurrent execution 14 a.release ©Magee/Kramer action relabeling ©Magee/Kramer action relabeling 5HODEHOLQJIXQFWLRQVDUHDSSOLHGWRSURFHVVHVWRFKDQJH WKHQDPHVRIDFWLRQODEHOV7KHJHQHUDOIRUPRIWKH UHODEHOLQJIXQFWLRQLV ^QHZODEHOBROGODEHOB«QHZODEHOBQROGODEHOBQ` ||CLIENT_SERVER = (CLIENT || SERVER) /{call/request, reply/wait}. call 0 5HODEHOLQJWRHQVXUHWKDWFRPSRVHG SURFHVVHVV\QFKURQL]HRQSDUWLFXODUDFWLRQV reply 1 call SERVER CLIENT 0 2 1 2 reply continue call CLIENT_SERVER CLIENT = (call->wait->continue->CLIENT). SERVER = (request->service->reply->SERVER). service 0 service 1 reply 2 3 continue Concurrency: concurrent execution 15 ©Magee/Kramer Concurrency: concurrent execution 16 ©Magee/Kramer action relabeling - prefix labels action hiding - abstraction to reduce complexity $QDOWHUQDWLYHIRUPXODWLRQRIWKHFOLHQWVHUYHUV\VWHPLV GHVFULEHGEHORZXVLQJTXDOLILHGRUSUHIL[HGODEHOV SERVERv2 = (accept.request ->service->accept.reply->SERVERv2). CLIENTv2 = (call.request ->call.reply->continue->CLIENTv2). :KHQDSSOLHGWRDSURFHVV3WKHKLGLQJRSHUDWRU?^DD[` UHPRYHVWKHDFWLRQQDPHVDD[IURPWKHDOSKDEHWRI3 DQGPDNHVWKHVHFRQFHDOHGDFWLRQVVLOHQW7KHVHVLOHQW DFWLRQVDUHODEHOHGWDX6LOHQWDFWLRQVLQGLIIHUHQW SURFHVVHVDUHQRWVKDUHG 6RPHWLPHVLWLVPRUHFRQYHQLHQWWRVSHFLI\WKHVHWRI ODEHOVWREHH[SRVHG ||CLIENT_SERVERv2 = (CLIENTv2 || SERVERv2) /{call/accept}. Concurrency: concurrent execution :KHQ DSSOLHG WR D SURFHVV 3 WKH LQWHUIDFH RSHUDWRU #^DD[` KLGHV DOO DFWLRQV LQ WKH DOSKDEHWRI3QRWODEHOHGLQWKHVHWDD[ 17 Concurrency: concurrent execution 18 ©Magee/Kramer ©Magee/Kramer action hiding structure diagrams 7KHIROORZLQJGHILQLWLRQVDUHHTXLYDOHQW USER = (acquire->use->release->USER) \{use}. P tau P 0LQLPL]DWLRQ UHPRYHV KLGGHQ tau DFWLRQV WR 0 1 2 a c x m c x b d x 3DUDOOHO&RPSRVLWLRQ 3__4^PDPEFG` Q SURGXFH DQ /76 ZLWK HTXLYDOHQW S REVHUYDEOH EHKDYLRU x acquire P a release 0 Concurrency: concurrent execution 3URFHVV3ZLWK DOSKDEHW^DE` b USER = (acquire->use->release->USER) @{acquire,release}. acquire a Q y &RPSRVLWHSURFHVV __6 3__4#^[\` 1 release 19 ©Magee/Kramer Concurrency: concurrent execution 20 ©Magee/Kramer structure diagrams structure diagrams 6WUXFWXUHGLDJUDPIRUCLIENT_SERVER " :HXVHVWUXFWXUHGLDJUDPVWRFDSWXUHWKHVWUXFWXUH RIDPRGHOH[SUHVVHGE\WKHVWDWLFFRPELQDWRUV SDUDOOHOFRPSRVLWLRQUHODEHOLQJDQGKLGLQJ range T = 0..3 BUFF = (in[i:T]->out[i]->BUFF). CLIENT call continue wait TWOBUFF CLIENTv2 in out in a.out reply request reply b:BUFF in out call call accept continue out Concurrency: concurrent execution 21 service structure diagrams - resource sharing SERVERv2 service Concurrency: concurrent execution ©Magee/Kramer a:USER SERVER 6WUXFWXUHGLDJUDPIRUCLIENT_SERVERv2 " ||TWOBUF = ? a:BUFF call 22 ©Magee/Kramer 3.2 Multi-threaded Programs in Java Concurrency in Java occurs when more than one thread is alive. ThreadDemo has two threads which rotate displays. PRINTER_SHARE printer printer: RESOURCE b:USER acquire release printer RESOURCE = (acquire->release->RESOURCE). USER = (printer.acquire->use ->printer.release->USER). ||PRINTER_SHARE = (a:USER||b:USER||{a,b}::printer:RESOURCE). Concurrency: concurrent execution 23 ©Magee/Kramer Concurrency: concurrent execution 24 ©Magee/Kramer ThreadDemo model ThreadDemo implementation in Java - class diagram ThreadDemo creates two ThreadPanel displays when initialized. ThreadPanel manages the display and control buttons, and delegates calls to rotate() to DisplayThread. Rotator implements the runnable interface. THREAD_DEMO a.run a.pause b.run a:ROTATOR a.rotate b:ROTATOR stop b.pause Applet b.rotate Panel GraphicCanvas ThreadDemo ROTATOR = PAUSED, PAUSED = (run->RUN | pause->PAUSED |interrupt->STOP), RUN = (pause->PAUSED |{run,rotate}->RUN |interrupt->STOP). ||THREAD_DEMO = (a:ROTATOR || b:ROTATOR) /{stop/{a,b}.interrupt}. ,QWHUSUHW run A,B init() start() stop() pause, interrupt DV LQSXWV rotate DV DQ RXWSXW Concurrency: concurrent execution ThreadPanel display rotate() start() stop() thread Thread DisplayThread rotate() Runnable Rotator 25 Concurrency: concurrent execution target run() 26 ©Magee/Kramer ©Magee/Kramer Rotator class ThreadPanel class public class ThreadPanel extends Panel { class Rotator implements Runnable { // construct display with title and segment color c public ThreadPanel(String title, Color c) {…} public void run() { try { while(true) ThreadPanel.rotate(); } catch(InterruptedException e) {} } // rotate display of currently running thread 6 degrees // return value not used in this example public static boolean rotate() throws InterruptedException {…} } Calls to rotate() are delegated to DisplayThread. // create a new thread with target r and start it running public void start(Runnable r) { thread = new DisplayThread(canvas,r,…); thread.start(); Threads are created by } the start() method, and terminated by the // stop the thread using Thread.interrupt() public void stop() {thread.interrupt();} stop() method. Rotator implements the runnable interface, calling ThreadPanel.rotate() to move the display. run()finishes if an exception is raised by Thread.interrupt(). Concurrency: concurrent execution ThreadPanel manages the display and control buttons for a thread. 27 ©Magee/Kramer } Concurrency: concurrent execution 28 ©Magee/Kramer ThreadDemo class Summary public class ThreadDemo extends Applet { ThreadPanel A; ThreadPanel B; &RQFHSWV public void init() { A = new ThreadPanel("Thread A",Color.blue); B = new ThreadPanel("Thread B",Color.blue); add(A); add(B); ThreadDemo creates two } ThreadPanel displays public void start() { when initialized and two A.start(new Rotator()); threads when started. B.start(new Rotator()); } public void stop() { A.stop(); B.stop(); } Concurrency: concurrent execution } ThreadPanel is used extensively in later demonstration programs. z FRQFXUUHQW SURFHVVHV DQG SURFHVV LQWHUDFWLRQ 0RGHOV z $V\QFKURQRXV DUELWUDU\ VSHHG LQWHUOHDYLQJ DUELWUDU\ RUGHU z 3DUDOOHO FRPSRVLWLRQ DV D ILQLWH VWDWH SURFHVV ZLWK DFWLRQ LQWHUOHDYLQJ z 3URFHVV LQWHUDFWLRQ E\ VKDUHG DFWLRQV z 3URFHVV ODEHOLQJ DQG DFWLRQ UHODEHOLQJ DQG KLGLQJ z 6WUXFWXUH GLDJUDPV 3UDFWLFH 29 ©Magee/Kramer z 0XOWLSOH WKUHDGV LQ -DYD Concurrency: concurrent execution 30 ©Magee/Kramer