1 / 18

Parallel Programming

Parallel Programming. Thread Synchronization. Heute. Lösung zu Assignment 2 Erstellen und Starten von Threads in Java Das synchronized Schlüsselwort in Java Ausblick auf Assignment 3. Live. 1. – Assignment 2. 2. – Starten von Threads in java. Threads erstellen.

ervin
Télécharger la présentation

Parallel Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Parallel Programming Thread Synchronization

  2. Heute • Lösung zu Assignment 2 • Erstellen und Starten von Threads in Java • Das synchronized Schlüsselwort in Java • Ausblick auf Assignment 3

  3. Live 1. – Assignment 2

  4. 2. – Starten von Threads in java

  5. Threads erstellen Klasse java.lang.Thread Zwei Möglichkeiten: • Interface Runnable • Thread.run überschreiben

  6. Interface Runnable publicinterfaceRunnable{voidrun();} • //in MyProcess.java • publicclassMyProcessimplementsRunnable {voidrun() {//what I wantthisthreadto doSystem.out.println(“Hellofromthread“); } } //whereyoustartthethread MyProcess mp = newMyProcess(); Thread t = new Thread(mp); t.start();

  7. Thread.run überschreiben • //in MyProcess.java • publicclassMyProcessextends Thread {voidrun() {//what I want this thread to doSystem.out.println(“Hello from thread“); } • } • //where you start the thread • MyProcess mp = newMyProcess(); • mp.start();

  8. Was ist besser? Thread.run + Einfacher aufzusetzen + Mehr Kontrolle über das Thread-Objekt • Weniger flexibel (Vererbung) Runnable Interface + Steht Vererbung nicht im Weg + Ist nicht an Threads gebunden - Benötigt einen Schritt mehr zum Aufsetzen

  9. Threads auf Eis • try { //doze a random time (0 to 0.5 secs) tosimulateworkloadThread.sleep( (int)(Math.random()*500) );} catch (InterruptedException e) { … } • Thread.sleep(long) versetzen den aktuellen Thread für mindestens x Millisekunden in den sleep-Zustand. • Wozu InterruptedExcpetion?

  10. Ohne InterruptedException Thread A Thread B Thread.sleep(500) Katastrophe Zeit

  11. Mit InterruptedException Thread A Thread B Thread.sleep(500) Katastrophe Zeit

  12. 3. – synchronized

  13. „Intrinsic“ synchronized • publicclassBuffer {publicsynchronizedvoidwrite(int i) { … }publicsynchronizedintread() { … }} • Für Methoden • Thread muss zunächst exklusiven „lock“ erlangen • Jede Klasse und jedes Objekt hat einen „intrinsic lock“ Schliessen sich gegenseitig aus („mutual exclusion“), weil beide „this“ als „lock“ verwenden

  14. synchronized Block • publicvoidsomeMethod1(){//do somethingbeforesynchronized(anObject) { … }//do something after}publicvoidsomeMethod2() {//do somethingbeforesynchronized(anObject) { … }//do something after} Schliessen sich gegenseitig aus, weil beide das selbe Objekt als „lock“ verwenden

  15. Fragen • Können statische methoden (staic) auch synchronized werden? • Was wäre das „lock object“? • Wieso Objekte als „locks“? • und nicht z.B. Zahlen, oder Strings?

  16. Noch mehr Fragen • publicvoidaddCustomer(DB db, Customer c) {synchronized(db) {addAddress(db, c.Address);db.newCustomer(c); }}publicvoidaddAddress(DB database, Address a) {db.verifyAddress(a); • synchronized(database) {db.newAddress(db, a); }} Zweimal wird das DB-Objekt als „lock“ verwendet. Funktioniert das? Wieso? Wieso nicht?

  17. 4. – Assignment 3

  18. Das producer/consumer-Beispiel Producer Consumer write()‏ • Der „producer“ produziert ständig neue Werte und schreibt sie in einen gemeinsamen „buffer“ • Der „consumer“ liest die Werte aus dem gemeinsamen „buffer“ und verwendet sie • Jeder Wert darf nur genau einmal konsumiert werden. • Die Frage: Wie synchronisiert man die zwei? read()‏ write()‏ read()‏ Buffer write()‏ read()‏

More Related