Verteilte Software Java Threads 1 Object wait notify

  • Slides: 8
Download presentation
Verteilte Software - Java - Threads 1 Object wait() notify. All() Thread start() run()

Verteilte Software - Java - Threads 1 Object wait() notify. All() Thread start() run() sleep() join() yield() is. Alive() <<interface >> Runnable run() Prof. Dr. -Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik

Verteilte Software - Java - Threads 2 Thread start() run() sleep() join() Klasse_A <<implements>>

Verteilte Software - Java - Threads 2 Thread start() run() sleep() join() Klasse_A <<implements>> Klasse_B <<interface >> Runnable run() start() join() run() My. Thread <<includes>> run() Thread <<uses>> start() run() sleep() join() Prof. Dr. -Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik

Verteilte Software - Java - Threads 3 public class Thr_1 { static public void

Verteilte Software - Java - Threads 3 public class Thr_1 { static public void main (String args[]) { int i; My. Thread thread[]; thread = new My. Thread[5]; for (i = 0; i < 5; i++) { thread[i] = new My. Thread(i); thread[i]. start(); } try { for (i = 0; i < 5; i++) thread[i]. join(); } catch (Interrupted. Exception e) { System. out. println(e); } } } class My. Thread extends Thread { int id; My. Thread (int id) { this. id = id; } public void run() { int z; for (z = 0; z < 3; z++) try { sleep(Math. round(1000. 0 * Math. random())); System. out. println("My. Thread " + id + " cycle " + z); } catch (Interrupted. Exception e) { System. out. println(e); } } } My. Thread My. Thread 2 4 4 2 0 4 1 3 0 2 3 0 1 1 3 cycle cycle cycle cycle 0 0 1 1 0 2 0 0 1 2 1 2 2 Prof. Dr. -Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik

Verteilte Software - Java - Threads 4 public class Thr_2 { static public void

Verteilte Software - Java - Threads 4 public class Thr_2 { static public void main (String args[]) { int i; My. Run. Thread thread[]; thread = new My. Run. Thread [4]; for (i = 0; i < 4; i++) { thread[i] = new My. Run. Thread(i); thread[i]. start(); } try { for (i = 0; i < 4; i++) thread[i]. join(); } catch (Interrupted. Exception e) { System. out. println(e); } } } My. Thread My. Thread 3 1 0 1 2 3 3 0 0 2 1 2 cycle cycle cycle 0 0 0 1 2 1 2 2 class A { int a; A(int a) {this. a = a; } } class My. Run. Thread extends A implements Runnable { Thread thread; My. Run. Thread (int id) { super(id); thread = new Thread(this); } public void start() { thread. start(); } public void join() throws Interrupted. Exception { thread. join(); } public void run() { int z; for (z = 0; z < 3; z++) try { thread. sleep(Math. round (1000. 0 * Math. random())); System. out. println("My. Thread " + a + " cycle " + z); } catch (Interrupted. Exception e) { System. out. println(e); } } } Prof. Dr. -Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik

Verteilte Software - Java - Threads 5 public class Thr_3 { static public void

Verteilte Software - Java - Threads 5 public class Thr_3 { static public void main (String args[]) { Monitor behaelter; int i; Thread thread[]; behaelter = new Monitor(100, 200); thread = new Thread [5]; thread[0] = new Zu. Thread(70, 100, behaelter); thread[1] = new Zu. Thread(40, 300, behaelter); thread[2] = new Zu. Thread(30, 600, behaelter); thread[3] = new Ab. Thread(50, 500, behaelter); thread[4] = new Ab. Thread(90, 200, behaelter); for (i = 0; i < thread. length; i++) thread[i]. start(); try { for (i = 0; i < thread. length; i++) thread[i]. join(); } catch (Interrupted. Exception e) { System. out. println(e); } Zu Zu Zu Ab Zu 70 40 70 50 70 90 30 40 50 30 30 90 40 level level level level 70 110 180 130 200 110 140 180 130 160 190 100 140 } } Prof. Dr. -Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik

Verteilte Software - Java - Threads 6 class Zu. Thread extends Thread { Monitor

Verteilte Software - Java - Threads 6 class Zu. Thread extends Thread { Monitor b; int menge, intervall; Zu. Thread (int menge, intervall, Monitor b) { this. menge = menge; this. b = b; this. intervall = intervall; } public void run() { for (int i = 0; i < 3; i++) { try { sleep(intervall); } catch (Interrupted. Exception e) { System. out. println(e); } System. out. println ("Zu " + menge + " level " + b. zu(menge)); } } } class Ab. Thread extends Thread { Monitor b; int menge, intervall; Ab. Thread (int menge, intervall, Monitor b) { this. menge = menge; this. b = b; this. intervall = intervall; } public void run() { for (int i = 0; i < 2; i++) { try { sleep(intervall); } catch (Interrupted. Exception e) { System. out. println(e); } System. out. println ("Ab " + menge + " level " + b. ab(menge)); } } } class Monitor { private int min, max, level; Monitor(int min, int max) { this. min = min; this. max = max; level = 0; } synchronized int zu(int x) { while ((level + x) > max) try { wait(); } catch (Interrupted. Exception e) { System. out. println(e); } level += x; notify(); return level; } synchronized int ab(int x) { while ((level - x) < min) try { wait(); } catch (Interrupted. Exception e) { System. out. println(e); } level -= x; notify(); return level; } } Prof. Dr. -Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik

Verteilte Software - Java - Threads 7 public class Thr_5 { static public void

Verteilte Software - Java - Threads 7 public class Thr_5 { static public void main(String args[]) { Manager m = new Manager(); int i; Philosoph phil[] = new Philosoph[Manager. N]; for (i = 0; i < phil. length; i++) phil[i] = new Philosoph(i, m); for (i = 0; i < phil. length; i++) phil[i]. start(); try { for (i = 0; i < phil. length; i++) phil[i]. join(); } catch (Interrupted. Exception e) { System. out. println(e); } } es es es es es essen essen essen essen essen 0 0 4 1 3 0 0 4 1 1 2 2 4 3 2 3 } Prof. Dr. -Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik

Verteilte Software - Java - Threads 8 class Philosoph extends Thread { Manager m;

Verteilte Software - Java - Threads 8 class Philosoph extends Thread { Manager m; int num; Philosoph (int num, Manager m) { this. num = num; this. m = m; } void denken() { try { sleep(800); } catch(Interrupted. Exception e) {}; } void essen() { try { sleep(200); } catch Interrupted. Exception e) {}; } public void run() { for (int i = 0; i < 4; i++) { denken(); m. nehme. Gabeln(num); essen(); m. ablegen. Gabeln(num); } } } class Manager { public final static int N = 5; private int zustand_P[]; int i; Manager() { zustand_P = new int[N]; for (i = 0; i < zustand_P. length; i++) zustand_P[i] = 0; // denken } private int links(int pos){ return (pos + N - 1) % N; } private int rechts(int pos){ return (pos + 1) % N; } synchronized void nehme. Gabeln(int pos) { while ( zustand_P[links(pos)] == 1 // essen || zustand_P[rechts( pos)]== 1) // essen try { wait(); } catch (Interrupted. Exception e) {} zustand_P[pos] = 1; // essen zeige. Zustand(); } synchronized void ablegen. Gabeln(int pos) { zustand_P[pos] = 0; // denken notify(); } synchronized void zeige. Zustand() { System. out. print("es essen "); for (i = 0; i < zustand_P. length; i++) if (zustand_P[i] == 1) System. out. print(" " + i); System. out. println(); } } Prof. Dr. -Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik