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.

Java threading

1,331 views

Published on

Brief introduction into some basic topics of Java threading.

  • DOWNLOAD THE BOOK INTO AVAILABLE FORMAT (New Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THE 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 THE 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 eBOOK .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookBOOK, 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, EBOOK, 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 THE 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 THE 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 THE Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THE BOOK INTO AVAILABLE FORMAT (New Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THE 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 THE 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 eBOOK .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookBOOK, 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, EBOOK, 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 THE 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 THE 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 THE Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD FULL eBOOK INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. doc eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. PDF eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. doc eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookeBOOK Crime, eeBOOK Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD FULL eBOOK INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. doc eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. PDF eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. doc eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookeBOOK Crime, eeBOOK Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

Java threading

  1. 1. ThreadProgrammierung in Java und einige Synchronisationsmechanismen
  2. 2. Multi-Tasking beiBetriebssystemenProgramme laufen quasi-parallel
  3. 3. 2 Kerne 4 Kerne 24 Kerne
  4. 4. Sie lernen kennen• Parallele Ausführung von Algorithmen• Gleichzeitiger Zugriff auf Ressourcen• Mechanismen zur Synchronisation• Methode / Best Practices
  5. 5. Parallele Algorithmen Parallele ParalleleVerarbeitung Verarbeitungvon Aufgaben von Daten(task decomposition) (data decomposition)
  6. 6. int[] data = initializeArray();for( int i = 0; i<data.length; i++ ){ data[i] = data[i] * 2;}
  7. 7. int[] data = initializeArray();for( int i = 0; i<data.length; i++ ){ data[i] = data[i] * 2;}
  8. 8. int[] data = initializeArray();for( int i = 0; i<data.length; i++ ){ data[i] = data[i] * 2;}Kern #1
  9. 9. int[] data = initializeArray();for( int i = 0; i<data.length; i++ ){ data[i] = data[i] * 2;} 0 1 2 3 4 5 6 7 8 9Kern #1
  10. 10. int[] data = initializeArray();for( int i = 0; i<data.length; i++ ){ data[i] = data[i] * 2;} 0 1 2 3 4 5 6 7 8 9Kern #1Kern #2
  11. 11. int[] data = initializeArray();for( int i = 0; i<data.length; i++ ){ data[i] = data[i] * 2;} 0 1 2 3 4Kern #1Kern #2
  12. 12. import java.lang.Thread;public class MyThread extends Thread{ public void run() { // Code an dieser Stelle wird // nebenläufig ausgeführt // sobald der Thread startet }} Der Thread muss noch durch Aufruf der Methode start() gestartet werden.
  13. 13. public static void main( String[] args ){ MyThread t = new MyThread(); t.start(); System.out.println(„Thread läuft“); t.join();}
  14. 14. public static void main( String[] args ){ MyThread t = new MyThread(); t.start(); System.out.println(„Thread läuft“); t.join();}
  15. 15. Thread #1public static void main( String[] args ){ MyThread t = new MyThread(); t.start(); System.out.println(„Thread läuft“); t.join();} Zeit
  16. 16. Thread #1public static void main( String[] args ){ MyThread t = new MyThread(); t.start(); System.out.println(„Thread läuft“); t.join();} Zeit
  17. 17. Thread #1public static void main( String[] args ){ MyThread t = new MyThread(); t.start(); System.out.println(„Thread läuft“); t.join();} Zeit
  18. 18. Thread #1public static void main( String[] args ){ MyThread t = new MyThread(); t.start(); System.out.println(„Thread läuft“); t.join();} Zeit
  19. 19. Thread #1public static void main( String[] args ){ MyThread t = new MyThread(); Thread #2 t.start(); public void run() { System.out.println(„Thread läuft“); t.join(); }} Zeit
  20. 20. Thread #1public static void main( String[] args ){ MyThread t = new MyThread(); Thread #2 t.start(); public void run() { System.out.println(„Thread läuft“); t.join(); }} Zeit
  21. 21. Thread #1public static void main( String[] args ){ MyThread t = new MyThread(); Thread #2 t.start(); public void run() { System.out.println(„Thread läuft“); t.join(); }} Zeit
  22. 22. class MyThread extends Thread{ private int[] data; private int start; private int end; public MyThread( int[] data, int start, int end ) { this.data = data; this.start = start; this.end = end; } public void run() { for( int i=start; i<end; i++ ) { data[i] = data[i] * 2; } }}
  23. 23. public static void main( String[] args ){ int[] data = initializeArray(); int length = data.length; int half = length / 2; MyThread t1 = new MyThread( data, 0, half ); MyThread t2 = new MyThread( data, half, length ); t1.start(); t2.start(); t1.join(); t2.join();}
  24. 24. Vergleich der Performance
  25. 25. Vergleich der Performancedata.length mit 1 Thread mit 2 Threads 100 2 ms 2 ms 1000 5 ms 5 ms 10,000 41 ms 31 ms 100,000 414 ms 254 ms 1,000,000 4130 ms 2093 ms10,000,000 41288 ms 26739 ms
  26. 26. int[] data = new int[10];data[0] = initialValue();for( int i = 1; i<data.length; i++ ){ data[i] = data[i-1] + 1;}
  27. 27. int[] data = new int[10];data[0] = initialValue();for( int i = 1; i<data.length; i++ ){ data[i] = data[i-1] + 1;} index 0 1 2 3 4 5 6 7 8 9 value 0 1 2 3 4 5 6 7 8 9
  28. 28. Paralleler Zugriff aufgemeinsame Ressourcen
  29. 29. public class BankAccount{ private int balance; public void deposit( int amount ) { int temp = this.balance + amount; this.balance = temp; }}
  30. 30. public class BankAccount { private int balance; public void deposit( int amount ) { int temp = this.balance + amount; this.balance = temp; } }Step Thread 1: deposit(100); Thread 2: deposit(200); balance 1 temp = this.balance + amount; temp = this.balance + amount; 0 2 this.balance = temp; 100 3 this.balance = temp; 200
  31. 31. public class BankAccount{ private int balance; public synchronized void deposit( int amount ) { int temp = this.balance + amount; this.balance = temp; }}
  32. 32. public class BankAccount{ private int balance; public synchronized void deposit( int amount ) { int temp = this.balance + amount; this.balance = temp; }}Step Thread 1: deposit(100); Thread 2: deposit(200); balance 1 temp = this.balance + amount; 0 2 this.balance = temp; 100 3 temp = this.balance + amount; 100 4 this.balance = temp; 300
  33. 33. public class BankAccount{ private int balance; public void deposit( int amount ) { synchronized( this ) { int temp = this.balance + amount; this.balance = temp; } }} Die synchronized Anweisung erzeugt einen Lock auf dem angegebenen Objekt zur Absicherung einer kritischen Region.
  34. 34. public class BankAccount{ private int balance; public void deposit( int amount ) { synchronized( this ) { int temp = this.balance + amount; this.balance = temp; } }}Ein wartender Thread hat keine Kontrolle mehr.Nur ein Thread kann die kritische Region betreten.
  35. 35. public class BankAccount{ private int balance; private Lock lock = new ReentrantLock(); public void deposit( int amount ) { boolean acquired = lock.tryLock( 500, TimeUnit.MILLISECONDS ); if( acquired ) { int temp = this.balance + amount; this.balance = temp; lock.unlock(); } else {} }}
  36. 36. Dead Lock Lock lock1 = new ReentrantLock(); Lock lock2 = new ReentrantLock(); Thread #1 Thread #2lock1.lock(); lock2.lock();lock2.lock(); lock1.lock();.... ...lock2.unlock(); lock1.unlock();lock1.unlock(); lock2.unlock();
  37. 37. Was es sonst noch gibt• Semaphore • erlauben einer festen Anzahl von Threads das Betreten einer kritischen Region • Connection Pools zu Datenbanken• Conditions • erlauben die Koordination zwischen Threads • typisch beim Producer / Consumer Muster
  38. 38. Methode• Parallele Algorithmen • Ohne Threads starten • Laufzeit des Programms analysieren • Kritische Bereiche verbessern durch parallele Verarbeitung• Paralleler Ressourcenzugriff • Mit gröbster Synchronisation beginnen • Programm analysieren: warten Threads zu lange auf Ressourcen? • Kritische Bereiche verbessern durch feinere Synchronisationsverfahren und Zeitüberschreitungen
  39. 39. Zusammenfassung und Ausblick• Parallele Algorithmen• Paralleler Ressourcenzugriff• Synchronisationsmechanismen• Race Conditions, Dead Locks• Verteilte Systeme• Map Reduce Algorithmen

×