Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Concurrency

2,197 views

Published on

Introduzione al Java memory model

Published in: Technology, News & Politics
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (Unlimited) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/qURD } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/qURD } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/qURD } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/qURD } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/qURD } ......................................................................................................................... Download doc Ebook here { https://soo.gd/qURD } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Concurrency

  1. 1. Java Memory Model <ul><li>Amdahl's law </li></ul><ul><li>Introduzione alla concorrenza </li></ul><ul><li>Java Memory Model </li></ul><ul><li>Supporto hardware alla concorrenza </li></ul><ul><li>Conclusioni </li></ul>
  2. 2. Amdahl's law <ul><li>“ For the past 30 years, computer performance has been driven by Moore's Law; from now on, it will be driven by Amdahl's Law. Writing code that effectively exploits multiple processors can be very challenging.” </li></ul><ul><ul><li>Doron Rajwan,
 Research Scientist, Intel Corp </li></ul></ul>
  3. 3. Amdahl's law
  4. 4. più CPU, più core, più thread <ul><li>Le architetture multiprocessore sono ormai comunissime anche nel settore Desktop </li></ul><ul><li>Nel 2007 la maggior parte dei desktop saranno attrezzati con CPU dual-core o quad-core </li></ul><ul><li>E se prima programmare multithread serviva soprattutto per ottimizzare l'uso della CPU, ora diventerà un obbligo: </li></ul><ul><ul><li>Serve il campo di grano, e non la donna incinta! </li></ul></ul><ul><li>A breve avremo cellulari biprocessore? :) </li></ul>
  5. 5. Cos'è il Java Memory Model? <ul><li>Dato lo statement </li></ul><ul><ul><ul><li>risposta = 42; </li></ul></ul></ul><ul><li>Il memory model si occupa di rispondere alla domanda “ In quali condizioni un thread che accede alla variabile risposta vedrà il valore 42?” </li></ul><ul><ul><li>Se la domanda vi sembra assurda, vuol dire che siete nella stanza giusta </li></ul></ul><ul><ul><li>Ci sono moltissimi motivi per cui un thread potrebbe non vedere il valore 42 DOPO che è stato scritto </li></ul></ul>
  6. 6. Java Concurrency 101 <ul><li>Prima di approfondire l'argomento JMM, ripassiamo un momento i concetti base della programmazione concorrente in Java </li></ul><ul><ul><li>Java è stato il primo linguaggio mainstream ad includere un completo supporto alla programmazione concorrente </li></ul></ul><ul><ul><li>La classe Thread , l'interfaccia Runnable , i metodi wait , notify e notifyAll di Object, le parole chiave synchronized e volatile sono presenti fin da Java 1.0 </li></ul></ul>
  7. 7. Java concurrency 101 <ul><li>MyThread extends Thread </li></ul><ul><ul><li>Thread t = new MyThread(); </li></ul></ul><ul><li>Mythread implements Runnable </li></ul><ul><ul><li>Thread t = new Thread(MyThread); </li></ul></ul><ul><li>t.start() </li></ul><ul><ul><li>Invoca il metodo run in un altro contesto di esecuzione </li></ul></ul>
  8. 8. Java concurrency 101 <ul><li>synchronized acquisisce il monitor dell'oggetto, se disponibile </li></ul><ul><li>Se non è disponibile, attende </li></ul><ul><li>Il monitor è una caratteristica della classe Object , per cui qualsiasi oggetto lo possiede </li></ul><ul><li>Un metodo synchronized nelle prime JVM era di un'ordine di grandezza più lento di un metodo “normale” </li></ul><ul><li>Ormai il costo dell'acquisizione del monitor uncontended è trascurabile </li></ul>
  9. 9. Java Concurrency in Java5 <ul><li>Java5 ha portato un intero nuovo package con decine di classi (java.util.concurrent) ed una revisione del Java Memory Model e delle specifiche dei Thread </li></ul><ul><ul><li>JSR 133 </li></ul></ul><ul><ul><ul><li>Thread Specification </li></ul></ul></ul><ul><ul><li>JSR 166 </li></ul></ul><ul><ul><ul><li>Java Concurrency </li></ul></ul></ul>
  10. 10. Java Thread Specification <ul><li>JSR 133 </li></ul><ul><ul><li>Revisionata la gestione dei thread e del Java memory Model </li></ul></ul><ul><li>Obiettivi </li></ul><ul><ul><li>Essere chiara e non ambigua </li></ul></ul><ul><ul><li>Rendere semplice lo sviluppo di applicazioni multithread corrette sotto ogni piattaforma </li></ul></ul><ul><ul><li>Permettere la scrittura di HIGH Performance JVM </li></ul></ul><ul><li>Le spec precedenti di fatto vietavano ottimizzazioni che le JVM però facevano </li></ul>
  11. 11. Concurrency utilities <ul><li>JSR 166 </li></ul><ul><ul><li>Le primitive di sincronizzazione di Java sono troppo di basso livello </li></ul></ul><ul><ul><ul><li>Come fare un read-write lock? </li></ul></ul></ul><ul><ul><li>Con le primitive è troppo semplice commettere errori gravi </li></ul></ul><ul><ul><ul><li>Come fare un read-write lock corretto? :) </li></ul></ul></ul><ul><ul><li>Spesso quello che serve è una collection che sappia come comportarsi in un ambiente multi-thread </li></ul></ul><ul><ul><ul><li>La JSR 166 ne ha introdotte diverse </li></ul></ul></ul>
  12. 12. Concurrency utilities <ul><li>Executors </li></ul><ul><ul><li>Executor </li></ul></ul><ul><ul><li>ExecutorService </li></ul></ul><ul><ul><li>ScheduledExecutorService </li></ul></ul><ul><ul><li>Callable </li></ul></ul><ul><ul><li>Future </li></ul></ul><ul><ul><li>ScheduledFuture </li></ul></ul><ul><ul><li>Delayed </li></ul></ul><ul><ul><li>CompletionService </li></ul></ul><ul><ul><li>ThreadPoolExecutor </li></ul></ul><ul><ul><li>ScheduledThreadPoolExecutor </li></ul></ul><ul><ul><li>AbstractExecutorService </li></ul></ul><ul><ul><li>Executors </li></ul></ul><ul><ul><li>FutureTask </li></ul></ul><ul><ul><li>ExecutorCompletionService </li></ul></ul><ul><li>Queues </li></ul><ul><ul><li>BlockingQueue </li></ul></ul><ul><ul><li>ConcurrentLinkedQueue </li></ul></ul><ul><ul><li>LinkedBlockingQueue </li></ul></ul><ul><ul><li>ArrayBlockingQueue </li></ul></ul><ul><ul><li>SynchronousQueue </li></ul></ul><ul><ul><li>PriorityBlockingQueue </li></ul></ul><ul><ul><li>DelayQueue </li></ul></ul><ul><li>Concurrent Collections </li></ul><ul><ul><li>ConcurrentMap </li></ul></ul><ul><ul><li>ConcurrentHashMap </li></ul></ul><ul><ul><li>CopyOnWriteArray{List,Set} </li></ul></ul><ul><li>Synchronizers </li></ul><ul><ul><li>CountDownLatch </li></ul></ul><ul><ul><li>Semaphore </li></ul></ul><ul><ul><li>Exchanger </li></ul></ul><ul><ul><li>CyclicBarrier </li></ul></ul><ul><li>Timing </li></ul><ul><ul><li>TimeUnit </li></ul></ul><ul><li>Locks </li></ul><ul><ul><li>Lock </li></ul></ul><ul><ul><li>Condition </li></ul></ul><ul><ul><li>ReadWriteLock </li></ul></ul><ul><ul><li>AbstractQueuedSynchronizer </li></ul></ul><ul><ul><li>LockSupport </li></ul></ul><ul><ul><li>ReentrantLock </li></ul></ul><ul><ul><li>ReentrantReadWriteLock </li></ul></ul><ul><li>Atomics </li></ul><ul><ul><li>Atomic[Type], Atomic[Type]Array </li></ul></ul><ul><ul><li>Atomic[Type]FieldUpdater </li></ul></ul><ul><ul><li>Atomic{Markable,Stampable}Reference </li></ul></ul>
  13. 13. Thread safe: definizione <ul><li>Una classe è thread-safe quando si comporta correttamente se acceduta da thread multipli, a prescindere dallo scheduling dei thread e senza bisogno di ulteriore sincronizzazione da parte del codice chiamante </li></ul>
  14. 14. Test <ul><ul><li>Dopo la join dei due thread, </li></ul></ul><ul><ul><li>quale sarà l'output di i e j? </li></ul></ul>x = y = 0 x = 1 j = y Thread 1 y = 1 i = x Thread 2 start threads
  15. 15. Output possibile TEMPO 1 1 x = 1 j = y Thread 1 y = 1 i = x Thread 2
  16. 16. Output possibile TEMPO 1 0 x = 1 j = y Thread 1 y = 1 i = x Thread 2
  17. 17. Output possibile TEMPO 0 1 x = 1 j = y Thread 1 y = 1 i = x Thread 2
  18. 18. Ma anche questo! TEMPO 0 0 x = 1 j = y Thread 1 y = 1 i = x Thread 2
  19. 19. Che è successo? <ul><li>synchronized non si occupa solo dell'acquisizione del monitor (Atomicità) </li></ul><ul><li>I 3 aspetti di synchronized sono </li></ul><ul><ul><li>Atomicità </li></ul></ul><ul><ul><ul><li>Locking e mutua esclusione </li></ul></ul></ul><ul><ul><li>Visibilità </li></ul></ul><ul><ul><ul><li>Assicurarsi che i cambiamenti fatti da un thread siano visibili ad un altro thread </li></ul></ul></ul><ul><ul><li>Ordinamento </li></ul></ul><ul><ul><ul><li>Assicurazione sull'ordine di esecuzione degli statement </li></ul></ul></ul>
  20. 20. Data Race <ul><li>Dal punto di vista del JMM, se in un punto qualsiasi del codice le tre condizioni di seguito sono soddisfatte c'è un data race </li></ul><ul><ul><li>Un thread scrive una variabile </li></ul></ul><ul><ul><li>Un altro thread legge la variabile </li></ul></ul><ul><ul><li>I read e i write non sono ordinati </li></ul></ul><ul><li>Se c'è un data race, il programma non è correttamente sincronizzato. Se funziona, lo fa per sbaglio :) </li></ul>
  21. 21. Happens Before (HB) <ul><li>Il Java Memory Model definisce una relazione di ordinamento parziale chiamata “Happens Before” (HB) sulle azioni di un programma </li></ul><ul><ul><li>Una relazione di ordinamento parziale è algebricamente una relazione antisimmetrica, riflessiva e transitiva. </li></ul></ul><ul><ul><li>A differenza di una relazione di ordinamento totale, è possibile che alcuni elementi non siano in relazione con altri. </li></ul></ul><ul><ul><li>Java è meglio di C#. Smalltalk è meglio di Python. Fra C# e Python non ho preferenze </li></ul></ul>
  22. 22. Happens Before (HB) <ul><li>La JVM garantisce che una azione A sarà visibile ad una azione B se c'è una relazione di Happens Before: </li></ul><ul><ul><li>HB(A,B) </li></ul></ul><ul><li>Ci sono diverse regole per l'HB </li></ul><ul><ul><li>Program Order </li></ul></ul><ul><ul><li>Finalizer rule </li></ul></ul><ul><ul><li>Synchronization rule (lock, volatile, Thread start, Thread termination, Interruption) </li></ul></ul><ul><ul><li>Transitivity: se HB(x,y) e HB(y,z) -> HB(x,z) </li></ul></ul>
  23. 23. Happens Before (HB)? <ul><li>Cosa vuol dire in pratica? </li></ul><ul><li>Se non c'è una relazione HB(A,B), i compilatori possono ottimizzare e riordinare gli statement come meglio credono, lasciando invariato il risultato finale </li></ul><ul><ul><li>Ma anche modificando i risultati intermedi! </li></ul></ul><ul><li>Quindi per eliminare un data-race per due azioni A e B, si deve forzare un HB(A,B) </li></ul>
  24. 24. Interruzione di un thread <ul><li>class MyThread extends Thread { </li></ul><ul><li>public void run() { </li></ul><ul><li>while (!done) { // fai qualcosa ... } </li></ul><ul><li>} </li></ul><ul><li>public void setDone() { done = true ; } </li></ul><ul><li>private boolean done; </li></ul><ul><li>} </li></ul>
  25. 25. Interruzione di un thread <ul><li>Se un Thread mette la variabile done a true </li></ul><ul><li>Chi garantisce che un altro Thread veda il valore di done? </li></ul><ul><li>Se le due azioni (lettura e scrittura) non hanno una relazione di Happens Before, quindi non c'è nessuna garanzia che ciò avvenga mai </li></ul><ul><li>E' dunque teoricamente possibile – seppur improbabile - che il Thread non esca mai, soprattutto in un'architettura SMP! </li></ul>
  26. 26. Interruzione di un thread <ul><li>Affinchè il codice sia corretto, bisogna instaurare una relazione di HB fra la lettura e la scrittura, evitando così il data-race </li></ul><ul><ul><li>Usando un lock esplicito (synchronized o JSR 166) </li></ul></ul><ul><ul><li>Usando la keyword volatile </li></ul></ul><ul><ul><li>Usando una variabile atomica </li></ul></ul><ul><ul><ul><li>java.util.concurrent.atomic </li></ul></ul></ul>
  27. 27. Test: così funziona? <ul><ul><li>class MyThread extends Thread { </li></ul></ul><ul><ul><li>public void run() { </li></ul></ul><ul><ul><li>while (!done) { // fai qualcosa ... } </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public synchronized void setDone() { done = true ; } </li></ul></ul><ul><ul><li>private boolean done; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  28. 28. Test: così funziona? <ul><ul><li>class MyThread extends Thread { </li></ul></ul><ul><ul><li>public void run() { </li></ul></ul><ul><ul><li>while (!done) { // fai qualcosa ... } </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public synchronized void setDone() { done = true ; } </li></ul></ul><ul><ul><li>private boolean done; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  29. 29. Test: così funziona? <ul><li>Ovviamente no </li></ul><ul><li>Sincronizzare solo le scritture non instaura una relazione di HB fra le letture e le scritture </li></ul><ul><li>La lock rule dice infatti che un unlock Happens Before un lock sullo stesso monitor </li></ul><ul><li>Per evitare il data-race, bisogna sincronizzare anche le letture! </li></ul><ul><ul><li>o usare volatile che fa l'uno e l'altro </li></ul></ul>
  30. 30. Test: così funziona? <ul><ul><li>class MyThread extends Thread { </li></ul></ul><ul><ul><li>public void run() { </li></ul></ul><ul><ul><li>while (!done()) { // fai qualcosa ... } </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public synchronized void setDone() { </li></ul></ul><ul><ul><li>done = true ; } </li></ul></ul><ul><ul><li>private synchronized void done() { </li></ul></ul><ul><ul><li>return done; } </li></ul></ul><ul><ul><li>private boolean done; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  31. 31. Test: così funziona? <ul><ul><li>class MyThread extends Thread { </li></ul></ul><ul><ul><li>public void run() { </li></ul></ul><ul><ul><li>while (!done()) { // fai qualcosa ... } </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public synchronized void setDone() { </li></ul></ul><ul><ul><li>done = true ; } </li></ul></ul><ul><ul><li>private synchronized void done() { </li></ul></ul><ul><ul><li>return done; } </li></ul></ul><ul><ul><li>private boolean done; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  32. 32. Test: così funziona? <ul><li>Si, la variabile è correttamente sincronizzata </li></ul><ul><li>Si può però usare volatile che garantisce la stessa semantica per la visibilità e l'ordering </li></ul><ul><li>Ma, attenzione, non per l'atomicità! </li></ul><ul><li>Per un semplice test di uscita da un Thread, volatile è perfetta </li></ul>
  33. 33. Test: così funziona? <ul><ul><li>class MyThread extends Thread { </li></ul></ul><ul><ul><li>public void run() { </li></ul></ul><ul><ul><li>while (!done) { // fai qualcosa ... } </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public void setDone() { done = true ; } </li></ul></ul><ul><ul><li>private volatile boolean done; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  34. 34. CAS: supporto hardware <ul><li>CAS </li></ul><ul><ul><li>Compare and Swap </li></ul></ul><ul><ul><li>E' un istruzione Assembler comune nelle nuove CPU (Intel, AMD, Sparc) </li></ul></ul><ul><ul><li>PowerPc usa due istruzioni: load-linked e store-conditional </li></ul></ul><ul><li>E' uno dei motivi per cui oggi è possibile ottenere altissime performance nella programmazione concorrente </li></ul><ul><li>E' una sorta di optimistic-locking a livello hardware </li></ul>
  35. 35. CAS: supporto hardware <ul><li>CAS(Address, Expected, New) </li></ul><ul><ul><li>Address = Indirizzo di memoria </li></ul></ul><ul><ul><li>Expected = Il valore atteso, ossia il vecchio valore </li></ul></ul><ul><ul><li>New = Il nuovo valore </li></ul></ul><ul><li>CAS(123456, 42, 127) </li></ul><ul><ul><li>Se all'indirizzo 123456 c'è 42, allora scriverà 127 </li></ul></ul><ul><ul><li>Altrimenti non fa nulla </li></ul></ul><ul><li>CAS ritorna il valore alla locazione Address, in entrambi i casi </li></ul>
  36. 36. CAS e JVM <ul><li>Supporto a basso livello per int, long e object references (la JVM usa uno spinlock su CPU senza CAS) </li></ul><ul><li>Atomic class ( AtomicInteger , AtomicLong , ecc. del package java.util.concurrent.atomic ) </li></ul><ul><li>La maggioranza delle classi in java.util.concurrent usano al loro interno le variabili atomiche al posto dei classici lock (synchronized) </li></ul><ul><li>Quindi può succedere, da Java5 in poi e su CPU di nuova generazione, di usare delle classi che sono multithreaded e thread-safe ma che non usano synchronized </li></ul>
  37. 37. Conclusioni <ul><li>Con i nuovi processori multi core scrivere applicazioni multithread sarà sempre più importante e comune </li></ul><ul><li>La programmazione multithread è però in assoluto la più difficile. In Java si ha un supporto come in nessuna altra piattaforma, ma la difficoltà resta. Soprattutto se si cerca di fare i furbi! </li></ul><ul><li>Con Java 5 è finalmente possibile scrivere una applicazione multithread corretta su tutte le piattaforme ed ad altissime performance sulla maggior parte di esse </li></ul>
  38. 38. Riferimenti <ul><li>JSR 133 </li></ul><ul><ul><li>www.cs.umd.edu/~pugh/java/memoryModel/ </li></ul></ul><ul><li>JSR 166 </li></ul><ul><ul><li>http://g.oswego.edu/dl/concurrency-interest/ </li></ul></ul><ul><li>Java Concurrency in practice </li></ul><ul><ul><li>Brian Goetz </li></ul></ul><ul><li>Concurrent programming in Java </li></ul><ul><ul><li>Doug Lea </li></ul></ul>

×