package Ġcom . alibaba . demo . coroutine Ċ Ċ import Ġkotlinx . coroutines .* Ċ import Ġorg . junit . Assert .* Ċ import Ġorg . junit . Test Ċ Ċ class ĠCor outine Thread Context Element Test Ġ{ ĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġthread Context Element _ pass By Value (): ĠUnit Ġ= Ġrun Blocking Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġmain Value Ġ= Ġ" main -${ System . currentTimeMillis () }" ĊĠĠĠĠĠĠĠ Ġval Ġlaunch Value Ġ= Ġ" launch -${ System . currentTimeMillis () }" ĊĠĠĠĠĠĠĠ Ġval Ġtest Thread Ġ= ĠThread . currentThread () ĊĊĠĠĠĠĠĠĠ Ġ// ĠString ĠThread Local , ĠString Ġis Ġimmutable Ġvalue , Ġcan Ġonly Ġbe Ġpassed Ġby Ġvalue ĊĠĠĠĠĠĠĠ Ġval Ġthread Local Ġ= ĠThread Local < String ?> () ĊĠĠĠĠĠĠĠ Ġthread Local . set ( main Value ) ĊĠĠĠĠĠĠĠ Ġprintln (" test Ġthread : Ġ${ Thread . currentThread ()}, Ġthread Ġlocal Ġvalue : Ġ${ thread Local . get () }") ĊĊĠĠĠĠĠĠĠ Ġval Ġjob Ġ= Ġlaunch ( Dispatch ers . Default Ġ+ Ġthread Local . as Context Element ( value Ġ= Ġlaunch Value )) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġprintln (" Launch Ġstart , Ġcurrent Ġthread : Ġ${ Thread . currentThread ()}, Ġthread Ġlocal Ġvalue : Ġ${ thread Local . get () }") ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( launch Value , Ġthread Local . get ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġassert Not Equals ( test Thread , ĠThread . currentThread ()) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġdelay ( 5 ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġprintln (" After Ġdelay , Ġcurrent Ġthread : Ġ${ Thread . currentThread ()}, Ġthread Ġlocal Ġvalue : Ġ${ thread Local . get () }") ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( launch Value , Ġthread Local . get ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġassert Not Equals ( test Thread , ĠThread . currentThread ()) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġreset Ġ= Ġ" job - reset -${ thread Local . get () }" ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthread Local . set ( reset ) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( reset , Ġthread Local . get ()) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġdelay ( 5 ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġprintln (" After Ġdelay Ġset Ġreset , Ġcurrent Ġthread : Ġ${ Thread . currentThread ()}, Ġthread Ġlocal Ġvalue : Ġ${ thread Local . get () }") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ// Ġ!!! ĠAfter Ġsus pended Ġdelay Ġfunction , Ġreset ĠThread Local Ġvalue Ġis Ġlost Ġ!!! ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ// ĠassertEquals ( reset , Ġthread Local . get ()) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( launch Value , Ġthread Local . get ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġassert Not Equals ( test Thread , ĠThread . currentThread ()) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġjob . join () ĊĊĠĠĠĠĠĠĠ Ġprintln (" after Ġlaunch , Ġtest Ġthread : Ġ${ Thread . currentThread ()}, Ġthread Ġlocal Ġvalue : Ġ${ thread Local . get () }") ĊĠĠĠĠĠĠĠ ĠassertEquals ( main Value , Ġthread Local . get ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġthread Context Element _ pass By Reference (): ĠUnit Ġ= Ġrun Blocking Ġ{ ĊĠĠĠĠĠĠĠ Ġdata Ġclass ĠReference ( var Ġdata : ĠInt Ġ= Ġ 4 2 ) ĊĊĠĠĠĠĠĠĠ Ġval Ġmain Value Ġ= ĠReference () ĊĠĠĠĠĠĠĠ Ġval Ġlaunch Value Ġ= ĠReference ( 4 2 4 2 ) ĊĠĠĠĠĠĠĠ Ġval Ġtest Thread Ġ= ĠThread . currentThread () ĊĊĠĠĠĠĠĠĠ Ġ// ĠReference ĠThread Local , Ġmutable Ġvalue , Ġpass Ġby Ġreference ĊĠĠĠĠĠĠĠ Ġval Ġthread Local Ġ= ĠThread Local < Reference >() Ġ// Ġdeclare Ġthread - local Ġvariable ĊĠĠĠĠĠĠĠ Ġthread Local . set ( main Value ) ĊĠĠĠĠĠĠĠ Ġprintln (" test Ġthread : Ġ${ Thread . currentThread ()}, Ġthread Ġlocal Ġvalue : Ġ${ thread Local . get () }") ĊĊĠĠĠĠĠĠĠ Ġval Ġjob Ġ= Ġlaunch ( Dispatch ers . Default Ġ+ Ġthread Local . as Context Element ( value Ġ= Ġlaunch Value )) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġprintln (" Launch Ġstart , Ġcurrent Ġthread : Ġ${ Thread . currentThread ()}, Ġthread Ġlocal Ġvalue : Ġ${ thread Local . get () }") ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( launch Value , Ġthread Local . get ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġassert Not Equals ( test Thread , ĠThread . currentThread ()) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġdelay ( 5 ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġprintln (" After Ġdelay , Ġcurrent Ġthread : Ġ${ Thread . currentThread ()}, Ġthread Ġlocal Ġvalue : Ġ${ thread Local . get () }") ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( launch Value , Ġthread Local . get ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġassert Not Equals ( test Thread , ĠThread . currentThread ()) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġreset Ġ= Ġ- 4 2 ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthread Local . get (). data Ġ= Ġreset ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġdelay ( 5 ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġprintln (" After Ġdelay Ġset Ġreset , Ġcurrent Ġthread : Ġ${ Thread . currentThread ()}, Ġthread Ġlocal Ġvalue : Ġ${ thread Local . get () }") ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( Reference ( reset ), Ġthread Local . get ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġassert Not Equals ( test Thread , ĠThread . currentThread ()) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġjob . join () ĊĊĠĠĠĠĠĠĠ Ġprintln (" after Ġlaunch , Ġtest Ġthread : Ġ${ Thread . currentThread ()}, Ġthread Ġlocal Ġvalue : Ġ${ thread Local . get () }") ĊĠĠĠĠĠĠĠ ĠassertEquals ( main Value , Ġthread Local . get ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtwo Thread Context Element (): ĠUnit Ġ= Ġrun Blocking Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġmain Value Ġ= Ġ" main - a -${ System . currentTimeMillis () }" ĊĠĠĠĠĠĠĠ Ġval Ġtest Thread Ġ= ĠThread . currentThread () ĊĠĠĠĠĠĠĠ Ġval Ġanother Main Value Ġ= Ġ" main - another -${ System . currentTimeMillis () }" ĊĊĠĠĠĠĠĠĠ Ġval Ġthread Local Ġ= ĠThread Local < String ?> () Ġ// Ġdeclare Ġthread - local Ġvariable ĊĠĠĠĠĠĠĠ Ġval Ġanother Thread Local Ġ= ĠThread Local < String ?> () Ġ// Ġdeclare Ġthread - local Ġvariable ĊĊĠĠĠĠĠĠĠ Ġthread Local . set ( main Value ) ĊĠĠĠĠĠĠĠ Ġanother Thread Local . set ( another Main Value ) ĊĠĠĠĠĠĠĠ Ġprintln (" test Ġthread : Ġ${ Thread . currentThread ()}, Ġthread Ġlocal Ġvalue : Ġ${ thread Local . get ()} Ġ| Ġ${ another Thread Local . get () }") ĊĊĠĠĠĠĠĠĠ Ġprintln () ĊĠĠĠĠĠĠĠ Ġval Ġlaunch 1 Value Ġ= Ġ" a Launch 1 " ĊĠĠĠĠĠĠĠ Ġlaunch ( Dispatch ers . Default Ġ+ Ġthread Local . as Context Element ( value Ġ= Ġlaunch 1 Value )) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġprintln (" Launch Ġstart , Ġcurrent Ġthread : Ġ${ Thread . currentThread ()}, Ġthread Ġlocal Ġvalue : Ġ${ thread Local . get ()} Ġ| Ġ${ another Thread Local . get () }") ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( launch 1 Value , Ġthread Local . get ()) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertNull ( another Thread Local . get ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġassert Not Equals ( test Thread , ĠThread . currentThread ()) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġdelay ( 5 ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġprintln (" After Ġdelay , Ġcurrent Ġthread : Ġ${ Thread . currentThread ()}, Ġthread Ġlocal Ġvalue : Ġ${ thread Local . get ()} Ġ| Ġ${ another Thread Local . get () }") ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( launch 1 Value , Ġthread Local . get ()) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertNull ( another Thread Local . get ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġassert Not Equals ( test Thread , ĠThread . currentThread ()) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġreset A Ġ= Ġ" job - reset -${ thread Local . get () }" ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthread Local . set ( reset A ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġreset Another Ġ= Ġ" job - reset -${ another Thread Local . get () }" ĊĠĠĠĠĠĠĠĠĠĠĠ Ġanother Thread Local . set ( reset Another ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġprintln (" Before Ġdelay Ġset Ġreset , Ġcurrent Ġthread : Ġ${ Thread . currentThread ()}, Ġthread Ġlocal Ġvalue : Ġ${ thread Local . get ()} Ġ| Ġ${ another Thread Local . get () }") ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġdelay ( 5 ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġprintln (" After Ġdelay Ġset Ġreset , Ġcurrent Ġthread : Ġ${ Thread . currentThread ()}, Ġthread Ġlocal Ġvalue : Ġ${ thread Local . get ()} Ġ| Ġ${ another Thread Local . get () }") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ// Ġ!!! ĠAfter Ġsus pended Ġdelay Ġfunction , Ġreset ĠThread Local Ġvalue Ġis Ġlost Ġ!!! ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ// ĠassertEquals ( reset A , Ġthread Local . get ()) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( launch 1 Value , Ġthread Local . get ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ// Ġ!!! ĠAfter Ġsus pended Ġdelay , ĠThread Local Ġwithout ĠThread Context Element Ġis Ġnot Ġclear Ġ!!! ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( reset Another , Ġanother Thread Local . get ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġassert Not Equals ( test Thread , ĠThread . currentThread ()) ĊĠĠĠĠĠĠĠ Ġ}. join () ĊĊĠĠĠĠĠĠĠ Ġprintln (" after Ġlaunch 1 , Ġtest Ġthread : Ġ${ Thread . currentThread ()}, Ġthread Ġlocal Ġvalue : Ġ${ thread Local . get ()} Ġ| Ġ${ another Thread Local . get () }") ĊĠĠĠĠĠĠĠ ĠassertEquals ( main Value , Ġthread Local . get ()) ĊĠĠĠĠĠĠĠ ĠassertEquals ( another Main Value , Ġanother Thread Local . get ()) ĊĊĠĠĠĠĠĠĠ Ġprintln () ĊĠĠĠĠĠĠĠ Ġval Ġlaunch 2 Value Ġ= Ġ" a Launch 2 " ĊĠĠĠĠĠĠĠ Ġval Ġanother Launch 2 Value Ġ= Ġ" another Launch 2 " ĊĠĠĠĠĠĠĠ Ġlaunch ( Dispatch ers . Default Ġ+ Ġthread Local . as Context Element ( value Ġ= Ġlaunch 2 Value ) Ġ+ Ġanother Thread Local . as Context Element ( value Ġ= Ġanother Launch 2 Value )) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġprintln (" Launch Ġstart , Ġcurrent Ġthread : Ġ${ Thread . currentThread ()}, Ġthread Ġlocal Ġvalue : Ġ${ thread Local . get ()} Ġ| Ġ${ another Thread Local . get () }") ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( launch 2 Value , Ġthread Local . get ()) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( another Launch 2 Value , Ġanother Thread Local . get ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġassert Not Equals ( test Thread , ĠThread . currentThread ()) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġdelay ( 5 ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġprintln (" After Ġdelay , Ġcurrent Ġthread : Ġ${ Thread . currentThread ()}, Ġthread Ġlocal Ġvalue : Ġ${ thread Local . get ()} Ġ| Ġ${ another Thread Local . get () }") ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( launch 2 Value , Ġthread Local . get ()) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( another Launch 2 Value , Ġanother Thread Local . get ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġassert Not Equals ( test Thread , ĠThread . currentThread ()) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġreset A Ġ= Ġ" job - reset -${ thread Local . get () }" ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthread Local . set ( reset A ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġreset Another Ġ= Ġ" job - reset -${ another Thread Local . get () }" ĊĠĠĠĠĠĠĠĠĠĠĠ Ġanother Thread Local . set ( reset Another ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġprintln (" Before Ġdelay Ġset Ġreset , Ġcurrent Ġthread : Ġ${ Thread . currentThread ()}, Ġthread Ġlocal Ġvalue : Ġ${ thread Local . get ()} Ġ| Ġ${ another Thread Local . get () }") ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġdelay ( 5 ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġprintln (" After Ġdelay Ġset Ġreset , Ġcurrent Ġthread : Ġ${ Thread . currentThread ()}, Ġthread Ġlocal Ġvalue : Ġ${ thread Local . get ()} Ġ| Ġ${ another Thread Local . get () }") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ// Ġ!!! ĠAfter Ġsus pended Ġdelay Ġfunction , Ġreset ĠThread Local Ġvalue Ġis Ġlost Ġ!!! ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ// ĠassertEquals ( reset A , Ġthread Local . get ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ// ĠassertEquals ( reset Another , Ġanother Thread Local . get ()) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( launch 2 Value , Ġthread Local . get ()) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( another Launch 2 Value , Ġanother Thread Local . get ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġassert Not Equals ( test Thread , ĠThread . currentThread ()) ĊĠĠĠĠĠĠĠ Ġ}. join () ĊĊĠĠĠĠĠĠĠ Ġprintln (" after Ġlaunch 2 , Ġtest Ġthread : Ġ${ Thread . currentThread ()}, Ġthread Ġlocal Ġvalue : Ġ${ thread Local . get ()} Ġ| Ġ${ another Thread Local . get () }") ĊĠĠĠĠĠĠĠ ĠassertEquals ( main Value , Ġthread Local . get ()) ĊĠĠĠĠĠĠĠ ĠassertEquals ( another Main Value , Ġanother Thread Local . get ()) ĊĠĠĠ Ġ} Ċ } Ċ