‍.’.ƒ…¾€¾»¾² " °·±¾€ ”— â„–1"

  • View
    1.768

  • Download
    9

Embed Size (px)

DESCRIPTION

О.В.Сухорослов "Разбор ДЗ №1", 30.03.2012, место показа МФТИ, Школа анализа данных (ШАД)

Text of ‍.’.ƒ…¾€¾»¾²...

  • 1. 06 1 .. oleg.sukhoroslov@gmail.com 30.03.2011.. () 06 1 30.03.2011 1 / 32

2. 1.1 - .. () 06 1 30.03.2011 2 / 32 3. Deadlock .. () 06 1 30.03.2011 3 / 32 4. .. () 06 130.03.2011 4 / 32 5. .. () 06 1 30.03.2011 5 / 32 6. tryLock() .. () 06 1 30.03.2011 6 / 32 7. - - N-1, .. () 06 130.03.2011 7 / 32 8. Busy wait (check, sleep... check, sleep...) (wait/notify) .. () 06 130.03.2011 8 / 32 9. ( - ) .. () 06 130.03.2011 9 / 32 10. 1 public class Fork extends ReentrantLock {} 2 ... 3 boolean hungry = true ; 4 while ( hungry ) { 5 left . lock (); 6 try { 7if ( right . tryLock ()) { 8 try { 9 eat ();10 hungry = false ;11 } finally {12 right . unlock ();13 }14}15 } finally {16left . unlock ();17 }18 if ( hungry ) {19try {20 Thread . sleep (10 + rnd . nextInt (10));21} catch ( I n t e r r u p t e d E x c e p t i o n e ) { break ; }22 }23 }.. () 06 130.03.2011 10 / 32 11. 1 think (); 2 synchronized ( TABLE ) { 3state = " HUNGRY " ; 4while (! canEat ()) { 5 try { 6TABLE . wait (); 7 } catch ( I n t e r r u p t e d E x c e p t i o n e ) { 8break ; 9 }10}11state = " EATING " ;12 }13 eat ();14 synchronized ( TABLE ) {15state = " THINKING " ;16TABLE . notifyAll ();17 }.. ()06 130.03.2011 11 / 32 12. 1 private boolean canEat () { 2 if ( LEFT . state . equals ( " EATING " ) || 3 RIGHT . state . equals ( " EATING " )) { 4return false ; 5 } 6 if ( LEFT . state . equals ( " HUNGRY " ) && 7 eatCount - LEFT . eatCount > 10) { 8return false ; 9 }10 if ( RIGHT . state . equals ( " HUNGRY " ) &&11 eatCount - RIGHT . eatCount > 10) {12return false ;13 }14 return true ;15 } .. () 06 130.03.2011 12 / 32 13. 1 think (); 2 philLock . lock (); 3 try { 4 waiter . makeOrder ( this ); 5 while (! canEat ) 6 condition . await (); 7 eat (); 8 canEat = false ; 9 waiter . thanks ( this );10 } catch ( I n t e r r u p t e d E x c e p t i o n e ) {11 break ;12 } finally {13 philLock . unlock ();14 }.. () 06 1 30.03.2011 13 / 32 14. concurrency Actors Software Transactional Memory (STM).. () 06 1 30.03.2011 14 / 32 15. 1Actors - , () Erlang, Io, Scala Akka (Java, Scala) http://akka.io/1http://en.wikipedia.org/wiki/Actor_model.. () 06 1 30.03.2011 15 / 32 16. Chandy-Misrahttp://www.cs.utexas.edu/users/misra/scannedPdf.dir/DrinkingPhil.pdf .. () 06 130.03.2011 16 / 32 17. Fork 1 public class Fork extends UntypedActor { 2 ... 3 public void onReceive ( Object msg ) throws Exception { 4ActorRef phil = getContext (). getSender (). get (); 5CompletableFuture < Object > reply = null ; 6if ( getContext (). getSenderFuture (). isDefined ()) { 7 reply = getContext (). getSenderFuture (). get (); 8} 9if ( msg instanceof String ) {10 String msg_ = ( String ) msg ;11 if ( msg_ . equals ( TAKE )) {12if (! used ) {13 if ( owner == null ) {14owner = phil ;15 } else if ( phil == owner ) {16reply . co m pl e te W it hR e su l t ( true );17 } else {18if (! clean ) {19clean = true ;20owner = phil ;21reply . co m pl e te W it h Re su l t ( true );22} else {23reply . co m pl e te W it h Re su l t ( false );24}25 }26} else {27 if ( promise == null ) {28promise = phil ;29promiseReply = reply ;30 } else {31reply . co m pl e te W it hR e su l t ( false );32 }.. ()06 130.03.2011 17 / 32 18. Fork (2) 1} else if ( msg_ . equals ( USE )) { 2if ( phil == owner ) { 3 used = true ; 4 reply . co m pl et e Wi t hR e su l t ( true ); 5} else { 6 reply . co m pl et e Wi t hR e su l t ( false ); 7} 8} else if ( msg_ . equals ( USED )) { 9if ( phil == owner ) {10 used = false ;11 clean = false ;12 if ( promise != null ) {13clean = true ;14owner = promise ;15promise = null ;16promiseReply . co m pl e te W it h Re s ul t ( true );17 }18}19} else if ( msg_ . equals ( NOT_USED )) {20if ( phil == owner ) {21 used = false ;22 if ( promise != null ) {23clean = true ;24owner = promise ;25promise = null ;26promiseReply . co m pl e te W it h Re s ul t ( true );27 }28}29} else throw new I l l e g a l A r g u m e n t E x c e p t i o n ( " Unknown message : " + msg );30} else throw new I l l e g a l A r g u m e n t E x c e p t i o n ( " Unknown message : " + msg );31}32 } .. () 06 130.03.201118 / 32 19. Philosopher 1 public class Philosopher extends UntypedActor { 2 ... 3 public Philosopher ( int id , ActorRef left , ActorRef right ) { 4 ... 5 if ( id == 0) { 6left . sendOneWay ( Fork . TAKE , me ); 7right . sendOneWay ( Fork . TAKE , me ); 8 } else if ( id != size -1) { 9left . sendOneWay ( Fork . TAKE , me );10 }11 }.. ()06 130.03.2011 19 / 32 20. Philosopher (2) 1public void onReceive ( Object msg ) throws Exception { 2if ( msg instanceof String ) { 3 String msg_ = ( String ) msg ; 4 if ( msg_ . equals ( THINK )) { 5System . out . println ( " [ " + id + " ] Thinking " ); 6Scheduler . scheduleOnce ( me , START_EAT , 0 , TimeUnit . MILLISECONDS ); 7 } else if ( msg_ . equals ( START_EAT )) { 8if (( Boolean ) left . sendRequestReply ( Fork . TAKE , me ) 9&& ( Boolean ) right . sendRequestReply ( Fork . TAKE , me )10&& ( Boolean ) left . sendRequestReply ( Fork . USE , me )11&& ( Boolean ) right . sendRequestReply ( Fork . USE , me )) {12 System . out . println ( " [ " + id + " ] Eating " );13 Scheduler . scheduleOnce ( me , FINISH_EAT , 0 , TimeUnit . MILLISECONDS );14} else {15 left . sendOneWay ( Fork . NOT_USED , me );16 Scheduler . scheduleOnce ( me , START_EAT , 0 , TimeUnit . MILLISECONDS );17}18 } else if ( msg_ . equals ( FINISH_EAT )) {19left . sendOneWay ( Fork . USED , me );20right . sendOneWay ( Fork . USED , me );21Scheduler . scheduleOnce ( me , THINK , 0 , TimeUnit . MILLISECONDS );22 } else throw new I l l e g a l A r g u m e n t E x c e p t i o n ( " [ " + id + " ] Unknown message : " + msg23} else throw new I l l e g a l A r g u m e n t E x c e p t i o n ( " [ " + id + " ] Unknown message : " + msg );24}25 } .. () 06 1 30.03.201120 / 32 21. Table 1 public class Table { 2 ... 3 public Table ( int philCount ) { 4 forks = new ArrayList < ActorRef >( philCount ); 5 for ( int i =0; i < philCount ; i ++) { 6 ActorRef fork = Actors . actorOf ( new ForkFactory ( i )); 7 forks . add ( fork ); 8 fork . start (); 9 }10 phils = new ArrayList < ActorRef >( philCount );11 for ( int i =0; i < philCount ; i ++) {12 ActorRef phil = Actors . actorOf (13new PhilFactory (i , ( i == philCount -1) ? forks . get (0) : forks . get ( i +1)) ,14 forks . get ( i ));15 phils . add ( phil );16 phil . start ();17 }18 }1920 public void start () {21 for ( ActorRef phil : phils ) {22 phil . sendOneWay ( Philosopher . THINK );23 }24 }2526 public void stop () {27for ( ActorRef phil : phils ) {28phil . stop ();29}30for ( ActorRef fork : forks ) {31fork . stop ();32}33.. } ()06 1 30.03.2011 21 / 32 22. Main 1 public static void main ( String [] args ) { 2 Table table = new Table (5); 3 table . start (); 4 5 try { 6 Thread . sleep (60000); 7 } catch ( I n t e r r u p t e d E x c e p t i o n e ) {} 8 9 table . stop ();10 } .. () 06 130.03.2011 22 / 32 23. 2Software Transactional Memory (STM) , Atomicity, Consistency, Isolation, (Durability) Optimistic concurrency Haskell, Clojure, ScalaSTM...2http://en.wikipedia.org/wiki/Software_transactional_memory.. () 06 130.03.2011 23 / 32 24. 3ScalaSTM 1 class Fork { val inUse = Ref ( false ) } 2 3 def meal ( left : Fork , right : Fork ) { 4 // thinking 5 atomic { implicit txn = > 6 if ( left . inUse () || right . inUse ()) 7 retry // forks are not both ready , wait 8 left . inUse () = true 9 right . inUse () = true10 }11 // eating12 atomic { implicit txn = >13 left . inUse () = false14 right . inUse () = false15 }16 } 3 http://nbronson.github.com/scala-stm/philosophers.html .. () 06 1 30.03.2011 24 / 32 25. 2.1 - .. () 06 1 30.03.2011 25 / 32 26. ?.. () 06 1 30.03.2011 26 / 32 27. CrawlerUtils.getLinks() VisualVM.. () 06 1 30.03.2011 27 / 32 28. ? 1 public static Set < URL > getLinks ( URL url , String content ) { 2 Set < URL > links = new HashSet < URL >(); 3 Matcher matcher = linkPattern . matcher ( content ); 4 while ( matcher . find ()) { 5 try { 6URL link = new URL ( url , matcher . group (1)); 7links . add ( link ); 8 } catch ( M a l f o r m e d U R L E x c e p t i o n e ) {} 9 }10 return links ;11 } .. () 06 1 30.03.2011 28 / 32 29. + set / / Executor , , , , - ? .. () 06 1 30.03.2011 29 / 32 30. ExecutorCompletionService4 5ExecutorService.awaitTermination()ExecutorService.invokeAll()ExecutorCompletionService Executor 6 executor.shutdown()4http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorCompletionService.html5http://stackoverow.com/questions/3269445/executorservice-how-to-wait-for-all-tasks-to-nish6 http://today.java.net/pub/a/today/2008/10/23/creating-a-notifying-blocking-thread-poolexecutor.html .. () 06 1 30.03.201130 / 32 31. Thread safe set CopyOnWriteArraySet ConcurrentSkipListSet Collections.synchronizedSet(Set s) Collections.newSetFromMap(new ConcurrentHashMap()) BueredWriter thread safe getContent() conn.setConnectTimeout(), conn.setReadTimeout().. () 06 1 30.03.2011 31 / 32 32. .. () 06 1 30.03.2011 32 / 32