Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java -

Similar presentations


Presentation on theme: "Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java -"— Presentation transcript:

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

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

3 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 main(String args[]) { int i; MyThread thread[]; thread = new MyThread[5]; for (i = 0; i < 5; i++) { thread[i] = new MyThread(i); thread[i].start(); } try { for (i = 0; i < 5; i++) thread[i].join(); } catch (InterruptedException e) { System.out.println(e); } } class MyThread extends Thread { int id; MyThread (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("MyThread " + id + " cycle " + z); } catch (InterruptedException e) { System.out.println(e); } } MyThread 2 cycle 0 MyThread 4 cycle 0 MyThread 4 cycle 1 MyThread 2 cycle 1 MyThread 0 cycle 0 MyThread 4 cycle 2 MyThread 1 cycle 0 MyThread 3 cycle 0 MyThread 0 cycle 1 MyThread 2 cycle 2 MyThread 3 cycle 1 MyThread 0 cycle 2 MyThread 1 cycle 1 MyThread 1 cycle 2 MyThread 3 cycle 2

4 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 main(String args[]) { int i; MyRunThread thread[]; thread = new MyRunThread [4]; for (i = 0; i < 4; i++) { thread[i] = new MyRunThread(i); thread[i].start(); } try { for (i = 0; i < 4; i++) thread[i].join(); } catch (InterruptedException e) { System.out.println(e); } } MyThread 3 cycle 0 MyThread 1 cycle 0 MyThread 0 cycle 0 MyThread 1 cycle 1 MyThread 2 cycle 0 MyThread 3 cycle 1 MyThread 3 cycle 2 MyThread 0 cycle 1 MyThread 0 cycle 2 MyThread 2 cycle 1 MyThread 1 cycle 2 MyThread 2 cycle 2 class A { int a; A(int a) {this.a = a;} } class MyRunThread extends A implements Runnable { Thread thread; MyRunThread (int id) { super(id); thread = new Thread(this); } public void start() { thread.start(); } public void join() throws InterruptedException { 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("MyThread " + a + " cycle " + z); } catch (InterruptedException e) { System.out.println(e); } }

5 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 main(String args[]) { Monitor behaelter; int i; Thread thread[]; behaelter = new Monitor(100, 200); thread = new Thread [5]; thread[0] = new ZuThread(70, 100, behaelter); thread[1] = new ZuThread(40, 300, behaelter); thread[2] = new ZuThread(30, 600, behaelter); thread[3] = new AbThread(50, 500, behaelter); thread[4] = new AbThread(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 (InterruptedException e) { System.out.println(e); } Zu 70 level 70 Zu 40 level 110 Zu 70 level 180 Ab 50 level 130 Zu 70 level 200 Ab 90 level 110 Zu 30 level 140 Zu 40 level 180 Ab 50 level 130 Zu 30 level 160 Zu 30 level 190 Ab 90 level 100 Zu 40 level 140

6 Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Threads 6 class ZuThread extends Thread { Monitor b; int menge, intervall; ZuThread (int menge, int 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 (InterruptedException e) { System.out.println(e); } System.out.println ("Zu " + menge + " level " + b.zu(menge)); } } class AbThread extends Thread { Monitor b; int menge, intervall; AbThread (int menge, int 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 (InterruptedException 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 (InterruptedException e) { System.out.println(e); } level += x; notify(); return level; } synchronized int ab(int x) { while ((level - x) < min) try { wait(); } catch (InterruptedException e) { System.out.println(e); } level -= x; notify(); return level; }

7 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 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 (InterruptedException e) { System.out.println(e); } es essen 0 es essen 0 2 es essen 4 es essen 1 es essen 3 es essen 0 es essen 0 2 es essen 4 es essen 1 4 es essen 1 3 es essen 0 es essen 0 2 es essen 4 es essen 1 es essen 1 3 es essen 0 es essen 0 2 es essen 4 es essen 1 es essen 1 3

8 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; int num; Philosoph (int num, Manager m) { this.num = num; this.m = m; } void denken() { try { sleep(800); } catch(InterruptedException e) {}; } void essen() { try { sleep(200); } catch InterruptedException e) {}; } public void run() { for (int i = 0; i < 4; i++) { denken(); m.nehmeGabeln(num); essen(); m.ablegenGabeln(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 nehmeGabeln(int pos) { while ( zustand_P[links(pos)] == 1 // essen || zustand_P[rechts(pos)]== 1) // essen try { wait();} catch (InterruptedException e) {} zustand_P[pos] = 1; // essen zeigeZustand(); } synchronized void ablegenGabeln(int pos) { zustand_P[pos] = 0; // denken notify(); } synchronized void zeigeZustand() { 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(); }


Download ppt "Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java -"

Similar presentations


Ads by Google