Adresre rekurzvne class Filter Adresara implements Filename Filter

  • Slides: 38
Download presentation
Adresáre rekurzívne class Filter. Adresara implements Filename. Filter { public boolean accept(File dir, String

Adresáre rekurzívne class Filter. Adresara implements Filename. Filter { public boolean accept(File dir, String name) { File f = new File(dir, name); return f. is. Directory(); rek. Vypis(System. get. Property("user. dir")); } } public class Podadresare. Rek { static void rek. Vypis(String aktualny. Adr) { String[] mena; File akt. Dir = new File(aktualny. Adr); Filter. Adresara Filter. Adr = new Filter. Adresara(); mena = akt. Dir. list(Filter. Adr); if (mena != null) { for (int i = 0; i < mena. length; i++) { String podadr = new String (aktualny. Adr + "\" + mena[i]); System. out. println(podadr); rek. Vypis(podadr); } }

Serializácia Object. Output. Stream File. Output. Stream out = new File. Output. Stream("AVL"); Object.

Serializácia Object. Output. Stream File. Output. Stream out = new File. Output. Stream("AVL"); Object. Output. Stream fs = new Object. Output. Stream(out); fs. write. Object("avl"); // String AVL: fs. write. Object(s); // AVLTree ¬í �t avlsr��AVLTree¡ 6û��G‰>� roott �L � LAVLNode; xpsr AVLNode�Ù=œö½â�� �I � x. L leftq � ~ �L rightq � ~ xp ssq � ~ � fs. flush(); ~ � sq ~ �ppsq�~ � Qsq ~ � ppsq @ ~ � bppsq ~ � • sq ~ �ppsq„ ~ �psq ¦~ � ·pp Object. Input. Stream File. Input. Stream in = new File. Input. Stream("AVL"); Object. Input. Stream is = new Object. Input. Stream(in); String str = (String)is. read. Object(); AVLTree ss = (AVLTree)is. read. Object(); Serializable Interface class AVLNode implements Serializable class AVLTree implements Serializable /sq

Typ enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }; … Days.

Typ enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }; … Days. SUNDAY …

enum public enum Planet { MERCURY (3. 303 e+23, 2. 4397 e 6), VENUS

enum public enum Planet { MERCURY (3. 303 e+23, 2. 4397 e 6), VENUS (4. 869 e+24, 6. 0518 e 6), EARTH (5. 976 e+24, 6. 37814 e 6), MARS (6. 421 e+23, 3. 3972 e 6); private final double mass; //[kg] private final double radius; //[m] Planet(double mass, double radius) { this. mass = mass; this. radius = radius; } //gravitational constant [m 3 kg-1 s-2] public static final double G = 6. 673 E-11; public double surface. Gravity() { return G * mass / (radius * radius); } }

enum public static void main(String[] args) { double earth. Weight = Double. parse. Double(args[0]);

enum public static void main(String[] args) { double earth. Weight = Double. parse. Double(args[0]); double mass = earth. Weight/EARTH. surface. Gravity(); for (Planet p : Planet. values()) { System. out. printf("Your weight on %s is %f%n", p, p. surface. Gravity()*mass); } } $ java Planet 175 Your weight on MERCURY is 66. 107583 Your weight on VENUS is 158. 374842 Your weight on EARTH is 175. 000000 Your weight on MARS is 66. 279007

Generics public interface List <E> { void add(E x); Iterator<E> iterator(); } public interface

Generics public interface List <E> { void add(E x); Iterator<E> iterator(); } public interface Iterator<E> { E next(); boolean has. Next(); } List<Integer>> public interface List { void add(List<Integer> x); Iterator<List<Integer>> iterator(); List<String> } public interface String. List { void add(String x); Iterator<String> iterator(); } public interface Integer. List { void add(Integer x); Iterator<Integer> iterator(); }

Generics Ak String je podtyp Object, aký je vzťah List(String) a List(Object) ? List<String>

Generics Ak String je podtyp Object, aký je vzťah List(String) a List(Object) ? List<String> ls = new Array. List<String>(); List<Object> lo = ls; lo. add(new Object()); String s = ls. get(0); List(String) nie je podtyp List(Object) !!!

Generics public class superclass { public void Too() { } } public class subclass

Generics public class superclass { public void Too() { } } public class subclass extends superclass { public void too() { } } public static void foo(superclass x) { } public static void goo(subclass x) { } public static superclass Hoo() { return new superclass(); } public static subclass hoo() { return new subclass(); } foo(new subclass()); goo(new superclass()); superclass supcl = hoo(); subclass subcl = Hoo(); hoo(). too(); hoo(). Too(); Hoo(). too(); Hoo(). Too();

Generics void print. Collection(Collection c) { Iterator i = c. iterator(); for (k =

Generics void print. Collection(Collection c) { Iterator i = c. iterator(); for (k = 0; k < c. size(); k++) System. out. println(i. next()); } void print. Collection(Collection<Object> c) { for (Object e : c) System. out. println(e); } Collection<Object> nie je supertype Collections void print. Collection(Collection<? > c) { for (Object e : c) System. out. println(e); }

Generics public abstract class Shape; public class Circle extends Shape; public class Rectangle extends

Generics public abstract class Shape; public class Circle extends Shape; public class Rectangle extends Shape; public void add. Rectangle(List<? extends Shape> shapes) { shapes. add(0, new Rectangle()); // Compile-time error! }

Reflection model Class c = subclass; Class c 1 = Class. for. Name("subclass"); Class

Reflection model Class c = subclass; Class c 1 = Class. for. Name("subclass"); Class c 2 = java. awt. Button. class; Class s = c. get. Superclass(); System. out. println(s. get. Name()); int m = c. get. Modifiers(); if (Modifier. is. Public(m)) System. out. println("public"); Class[] the. Interfaces = c. get. Interfaces(); for (int i = 0; i < the. Interfaces. length; i++) { String interface. Name = the. Interfaces[i]. get. Name(); System. out. println(interface. Name);

Premenné, konštruktory Field[] public. Fields = c. get. Fields(); for (int i = 0;

Premenné, konštruktory Field[] public. Fields = c. get. Fields(); for (int i = 0; i < public. Fields. length; i++) { String field. Name = public. Fields[i]. get. Name(); Class type. Class = public. Fields[i]. get. Type(); String field. Type = type. Class. get. Name(); System. out. println("Name: " + field. Name + ", Type: " + field. Type); } Constructor[] the. Constructors = c. get. Constructors(); for (int i = 0; i < the. Constructors. length; i++) { System. out. print("( "); Class[] parameter. Types = the. Constructors[i]. get. Parameter. Types(); for (int k = 0; k < parameter. Types. length; k ++) { String parameter. String = parameter. Types[k]. get. Name(); System. out. print(parameter. String + " "); } System. out. println(")"); }

Reflection – metódy Method[] the. Methods = c. get. Methods(); for (int i =

Reflection – metódy Method[] the. Methods = c. get. Methods(); for (int i = 0; i < the. Methods. length; i++) { String method. String = the. Methods[i]. get. Name(); System. out. println("Name: " + method. String); String return. String = the. Methods[i]. get. Return. Type(). get. Name(); System. out. println(" Return Type: " + return. String); } Class[] parameter. Types = the. Methods[i]. get. Parameter. Types(); System. out. print(" Parameter Types: "); for (int k = 0; k < parameter. Types. length; k ++) { String parameter. String = parameter. Types[k]. get. Name(); System. out. print(" " + parameter. String); } System. out. println();

Volanie konštruktora Class class. Definition = Class. for. Name(class. Name); Object object = class.

Volanie konštruktora Class class. Definition = Class. for. Name(class. Name); Object object = class. Definition. new. Instance(); Class rectangle. Definition = Class. for. Name("java. awt. Rectangle"); Class[] int. Args. Class = new Class[] {int. class, int. class}; Constructor int. Args. Constructor = rectangle. Definition. get. Constructor(int. Args. Class); Object[] int. Args = new Object[] {new Integer(12), new Integer(34)}; Rectangle rectangle = (Rectangle) create. Object(int. Args. Constructor, int. Args);

Volanie metódy public static String append(String first. Word, String second. Word) { String result

Volanie metódy public static String append(String first. Word, String second. Word) { String result = null; try { Class[] parameter. Types = new Class[] {String. class}; Class c = String. class; Method concat. Method = c. get. Method("concat", parameter. Types); Object[] arguments = new Object[] {second. Word}; result = (String) concat. Method. invoke(first. Word, arguments); } catch (Exception e) {. . } return result; }

Dnes bude • • • concurrency vs. parallelism threads alias processes synchronizácia kritická sekcia

Dnes bude • • • concurrency vs. parallelism threads alias processes synchronizácia kritická sekcia deadlock komunikácia (cez pipes)

Inšpirácia • http: //www. doc. ic. ac. uk/~jnm/book_applets/concurrency. html

Inšpirácia • http: //www. doc. ic. ac. uk/~jnm/book_applets/concurrency. html

package java. lang. Thread • • new Thread() alebo jeho subclass metóda run() Vytvorenie

package java. lang. Thread • • new Thread() alebo jeho subclass metóda run() Vytvorenie threadu public class Simple. Thread extends Thread { private int count. Down = 5; private static int thread. Count = 0; public Simple. Thread() { super("" + (++thread. Count)); start(); } public String to. String() { return "#" + get. Name() + ": " + count. Down; } public void run() { while(true) { System. out. println(this); if(--count. Down == 0) return; } } public static void main(String[] args) { for(int i = 0; i < 5; i++) new Simple. Thread(); } } #1: 5 #1: 4 #1: 3 #1: 2 #1: 1 #2: 5 #2: 4 #2: 3 #2: 2 #2: 1 #3: 5 #3: 4 #3: 3 #3: 2 #3: 1 #4: 5 #4: 4 #4: 3 #4: 2 #4: 1 #5: 5 #5: 4 #5: 3 #5: 2 #5: 1 #1: 5 #2: 5 #3: 5 #4: 5 #5: 4 #4: 4 #5: 3 #5: 2 #3: 4 #5: 1 #2: 4 #4: 3 #1: 4 #3: 3 #4: 2 #2: 3 #4: 1 #3: 2 #1: 3 #2: 2 #3: 1 #1: 2 #2: 1 #1: 1 public void run() { while(true) { System. out. println(this); for(int j=0; j<(5 -ind)*1000000 double gg = 0 -Math. PI+j+j-j if(--count. Down == 0) return; } }

 • yield() – daj šancu iným Uvoľnenie threadu public class Yielding. Thread extends

• yield() – daj šancu iným Uvoľnenie threadu public class Yielding. Thread extends Thread { private int count. Down = 5; private static int thread. Count = 0; #1: 5 #2: 5 public Yielding. Thread() { #3: 5 super("" + ++thread. Count); #4: 5 #5: 5 start(); #1: 4 } #2: 4 #3: 4 public String to. String() {. . . } #4: 4 public void run() { #5: 4 #1: 3 while(true) { #2: 3 System. out. println(this); #3: 3 #4: 3 if(--count. Down == 0) return; #5: 3 yield(); #1: 2 #2: 2 } #3: 2 } #4: 2 #5: 2 public static void main(String[] args) { #1: 1 for(int i = 0; i < 5; i++) #2: 1 #3: 1 new Yielding. Thread(); #4: 1 } #5: 1 }

MAX_PRIORITY MIN_PRIORITY NORM_PRIORITY set. Priority(int new. Priority) interval [1. . 10] Priority threadu public

MAX_PRIORITY MIN_PRIORITY NORM_PRIORITY set. Priority(int new. Priority) interval [1. . 10] Priority threadu public class Simple. Priorities extends Thread { private int count. Down = 5; private volatile double d = 0; // No optimization public Simple. Priorities(int priority) { set. Priority(priority); start(); } public void run() { while(true) { for(int i = 1; i < 100000; i++) d = d + (Math. PI + Math. E) / (double)i; System. out. println(this); if(--count. Down == 0) return; } } public static void main(String[] args) { new Simple. Priorities(Thread. MAX_PRIORITY); for(int i = 0; i < 5; i++) new Simple. Priorities(Thread. MIN_PRIORITY); } } [Thread-0, 10, main]: 5 [Thread-0, 10, main]: 4 [Thread-0, 10, main]: 3 [Thread-0, 10, main]: 2 [Thread-0, 10, main]: 1 [Thread-1, 1, main]: 5 [Thread-1, 1, main]: 4 [Thread-1, 1, main]: 3 [Thread-1, 1, main]: 2 [Thread-1, 1, main]: 1 [Thread-2, 1, main]: 5 [Thread-2, 1, main]: 4 [Thread-2, 1, main]: 3 [Thread-2, 1, main]: 2 [Thread-2, 1, main]: 1 [Thread-3, 1, main]: 5 [Thread-3, 1, main]: 4 [Thread-3, 1, main]: 3 [Thread-3, 1, main]: 2 [Thread-3, 1, main]: 1 [Thread-4, 1, main]: 5 [Thread-4, 1, main]: 4 [Thread-4, 1, main]: 3 [Thread-4, 1, main]: 2 [Thread-4, 1, main]: 1 [Thread-5, 1, main]: 5 [Thread-5, 1, main]: 4 [Thread-5, 1, main]: 3 [Thread-5, 1, main]: 2 [Thread-5, 1, main]: 1 [Thread-1, 10, main]: 5 [Thread-1, 10, main]: 4 [Thread-1, 10, main]: 3 [Thread-1, 10, main]: 2 [Thread-1, 10, main]: 1 [Thread-3, 8, main]: 5 [Thread-3, 8, main]: 4 [Thread-3, 8, main]: 3 [Thread-3, 8, main]: 2 [Thread-3, 8, main]: 1 [Thread-4, 7, main]: 5 [Thread-4, 7, main]: 4 [Thread-4, 7, main]: 3 [Thread-4, 7, main]: 2 [Thread-4, 7, main]: 1 [Thread-0, 1, main]: 5 [Thread-0, 1, main]: 4 [Thread-0, 1, main]: 3 [Thread-0, 1, main]: 2 [Thread-0, 1, main]: 1 [Thread-2, 1, main]: 5 [Thread-2, 1, main]: 4 [Thread-2, 1, main]: 3 [Thread-2, 1, main]: 2 [Thread-2, 1, main]: 1 [Thread-5, 2, main]: 5 [Thread-5, 2, main]: 4 [Thread-5, 2, main]: 3 [Thread-5, 2, main]: 2 [Thread-5, 2, main]: 1

sleep(long millis) throws Interrupted. Exception join() čakaj kým umre Uspatie threadu public class Sleeping.

sleep(long millis) throws Interrupted. Exception join() čakaj kým umre Uspatie threadu public class Sleeping. Thread extends Thread { private int count. Down = 5; private static int thread. Count = 0; public Sleeping. Thread() { … } public void run() { while(true) { System. out. println(this); if(--count. Down == 0) return; try { sleep(100); } catch (Interrupted. Exception e) { throw new Runtime. Exception(e); } } } public static void main(String[] args) throws Interrupted. Exception { for(int i = 0; i < 5; i++) new Sleeping. Thread(). join(); System. out. println("--"); } } #1: 5 #1: 4 #1: 3 #1: 2 #1: 1 -#2: 5 #2: 4 #2: 3 #2: 2 #2: 1 -#3: 5 #3: 4 #3: 3 #3: 2 #3: 1 -#4: 5 #4: 4 #4: 3 #4: 2 #4: 1 -#5: 5 #5: 4 #5: 3 #5: 2 #5: 1 --

Čakanie na thread class Sleeper extends Thread { private int duration; public Sleeper(String name,

Čakanie na thread class Sleeper extends Thread { private int duration; public Sleeper(String name, int sleep. Time) { super(name); duration = sleep. Time; start(); } public void run() { try { sleep(duration); } catch (Interrupted. Exception e) { System. out. println(get. Name() + " was interrupted. " + "is. Interrupted(): " + is. Interrupted()); return; } System. out. println(get. Name() + " has awakened"); } } Sleeper prvy = new Sleeper("Prvy", 1500); Sleeper druhy = new Sleeper("Druhy", 1500), Joiner treti = new Joiner("Treti", druhy), Joiner stvrty = new Joiner("Strvrty", prvy); prvy. interrupt(); class Joiner extends Thread { private Sleeper sleeper; public Joiner(String name, Sleeper sleeper) { super(name); this. sleeper = sleeper; start(); } public void run() { try { sleeper. join(); } catch (Interrupted. Exception e) { throw new Runtime. Exception(e); } System. out. println(get. Name() + " completed"); } } Prvy was interrupted. is. Interrupted(): false Strvrty completed Druhy has awakened Treti completed

Rúry class Sender extends Thread { class Receiver extends Thread { private Random rand

Rúry class Sender extends Thread { class Receiver extends Thread { private Random rand = new Random(); private Piped. Reader in; private Piped. Writer out = new Piped. Writer(); public Receiver(Sender sender) throws IOException { public Piped. Writer get. Piped. Writer() { return out; } in = new Piped. Reader(sender. get. Piped. Writer()); public void run() { } while(true) { public void run() { for(char c = 'A'; c <= 'z'; c++) { try { while(true) { out. write(c); System. out. println("Read: " + (char)in. read()); sleep(rand. next. Int(500)); } } catch(Exception e) { } catch(IOException e) { Read: A throw new Runtime. Exception(e); Read: B Read: C } } Read: D } } Read: E } } Read: F } Read: G } Read: H Read: I public class Piped. IO { Read: J public static void main(String[] args) throws Exception { Read: K Sender sender = new Sender(); Read: L Receiver receiver = new Receiver(sender); Read: M sender. start(); Read: N receiver. start(); Read: O Terminated new Timeout(4000, "Terminated"); Read: P } Read: Q } Read: R

public class Gulicka extends Thread { public int x =. . . , y

public class Gulicka extends Thread { public int x =. . . , y = …, dx = …, dy = … public void run() { while (true) { x += dx; y += dy; if (x >= size. X) { x = size. X - (x-size. X); dx = -dx; } else if (y >= size. Y) { y = size. Y - (y-size. Y); dy = -dy; } else if (x < 0) { x = -x; dx = -dx; } else if (y < 0) { y = -y; dy = -dy; } ap. repaint(); try { sleep(10); } catch(Exception e) {} } Guličky - thready

public class Ball extends Applet { Gulicka th 1, th 2; . . .

public class Ball extends Applet { Gulicka th 1, th 2; . . . public void start() { th 1 = new Gulicka (this); th 1. set. Priority(Thread. MAX_PRIORITY); th 1. start(); th 2 = new Gulicka (this); th 2. set. Priority(Thread. MIN_PRIORITY); th 2. start(); } public void update(Graphics g) { show. Status(th 1. steps + " " + th 2. steps); g. draw. Image(img, 0, 0, this); g. set. Color(Color. BLUE); g. draw. Rect(th 1. x, th 1. y, 2, 2); g. set. Color(Color. RED); g. draw. Rect(th 2. x, th 2. y, 2, 2); }. . } Guličky - applet

Sorty public class Sapplet extends Applet { Sort. Panel c 1, c 2, c

Sorty public class Sapplet extends Applet { Sort. Panel c 1, c 2, c 3, c 4; public void init() { set. Size(800, 200); set. Layout(new Grid. Layout(1, 4)); c 1 = new Sort. Panel("Buble", Color. MAGENTA); add(c 1); c 2 = new Sort. Panel("Quick", Color. BLUE); add(c 2); c 3 = new Sort. Panel("Merge", Color. RED); add(c 3); c 4 = new Sort. Panel("Random", Color. GREEN); add(c 4); } public void start() { c 1. start(); c 2. start(); c 3. start(); c 4. start(); } }

Sort. Panel, Canvas public class Sort. Panel extends Panel { Sort. Thread thread ;

Sort. Panel, Canvas public class Sort. Panel extends Panel { Sort. Thread thread ; Graphic. Canvas can; int[] a; . . public void start() { a = new int[100]; for(int i=0; i < a. length; i++) a[i] = (int)(200*Math. random()); thread = new Sort. Thread(can, algo, a); thread. start(); } } public class Graphic. Canvas extends Canvas { . . public void swap(int i, int j) { lo = i; hi = j; } public void update(Graphics g) { g. clear. Rect(0, 0, 200); Color cl = g. get. Color(); for(int i=0; i < sp. a. length; i++) { g. set. Color((i == lo || i == hi)? Color. BLACK: cl); g. draw. Line(2*i, sp. a[i], 2*i, 0); //g. draw. Line(2*i, sp. a[i], 2*i, sp. a[i]); } } }

public class Sort. Thread extends Thread { Graphic. Canvas can; public void run() {

public class Sort. Thread extends Thread { Graphic. Canvas can; public void run() { if (algo. equals("Buble")) buble. Sort(a); . . else random. Sort(a); } void swap(int i, int j) { can. swap(i, j); can. repaint(); try{ sleep(10); } catch(Exception e) {} } void random. Sort(int a[]) { while(true) { int i = (int)((a. length-1)*Math. random()); int j = (int)((a. length-1)*Math. random()); swap(i, j); if (i<j && a[i] > a[j]) { int pom = a[i]; a[i] = a[j]; a[j] = pom; } } } Random. S rt

Semafóry public class Semaphore implements Invariant { private volatile int semaphore = 0; public

Semafóry public class Semaphore implements Invariant { private volatile int semaphore = 0; public boolean available() { return semaphore == 0; } public void acquire() { ++semaphore; } public void release() { --semaphore; } public Invariant. State invariant() { int val = semaphore; return(val == 0 || val == 1)? new Invariant. OK(): new Invariant. Failure(new Integer(val)); } } java Semaphore. Tester Invariant violated: 2 public class Semaphore. Tester extends Thread { . . public void run() { while(true) if(semaphore. available()) { yield(); // skor to rachne semaphore. acquire(); yield(); semaphore. release(); yield(); } } public static void main(String[] args) throws Exception { Semaphore sem = new Semaphore(); new Semaphore. Tester(sem); new Invariant. Watcher(sem). join(); } }

Synchronizácia public class Synchronized. Semaphore extends Semaphore { private volatile int semaphore = 0;

Synchronizácia public class Synchronized. Semaphore extends Semaphore { private volatile int semaphore = 0; public synchronized boolean available() { return semaphore == 0; } public synchronized void acquire() { ++semaphore; } public synchronized void release() { --semaphore; } }. . . a teraz to už pojde ? public void run() { while(true) if(semaphore. available()) { semaphore. acquire(); semaphore. release(); } } - atomická operácia vs. kritická sekcia

Kritická sekcia Synchronizácia celej metódy public synchronized void do. Task() { p. increment. X();

Kritická sekcia Synchronizácia celej metódy public synchronized void do. Task() { p. increment. X(); p. increment. Y(); store(); } Kritická sekcia public void do. Task() { synchronized(this) { p. increment. X(); p. increment. Y(); } store(); }

Stavy threadu • • new – nenaštartovaný ešte runnable – može bežať, keď mu

Stavy threadu • • new – nenaštartovaný ešte runnable – može bežať, keď mu bude pridelený CPU dead – keď skončí metóda run(), resp. po stop() blocked – niečo mu bráni, aby bežal – – sleep(miliseconds) – počká daný čas, ak nie je interrupted. . . wait(), resp. wait (miliseconds) čaká na správu notify() resp. notify. All() , čaká na I/O, pokúša sa zavolať synchronized na metódu. resp. objekt ktorý je už v tom. . . sleep vs. wait keď proces volá wait(), výpočet je pozastavený, ale iné synchronizované metódy môžu byt volané

Semafóry znova public class Semaphore { private int value; public Semaphore(int val) { value

Semafóry znova public class Semaphore { private int value; public Semaphore(int val) { value = val; } public synchronized void acquire() { while (value == 0) try { wait(); } catch (Interrupted. Exception ie) { } value--; } public synchronized void release() { ++value; notify(); } java. util. concurrent. Semaphor }

Večerajúci filozofovia class Fork { private boolean taken=false; private Phil. Canvas display; private int

Večerajúci filozofovia class Fork { private boolean taken=false; private Phil. Canvas display; private int identity; Fork(Phil. Canvas disp, int id) { display = disp; identity = id; } synchronized void put() { taken=false; display. set. Fork(identity, taken); notify(); } synchronized void get() throws java. lang. Interrupted. Exception { while (taken) wait(); taken=true; display. set. Fork(identity, taken); } }

Večerajúci filozofovia 3 2 2 1 3 class Philosopher extends Thread { 4 4

Večerajúci filozofovia 3 2 2 1 3 class Philosopher extends Thread { 4 4 . . . public void run() { 0 try { while (true) { // thinking view. set. Phil(identity, view. THINKING); sleep(controller. sleep. Time()); // hungry view. set. Phil(identity, view. HUNGRY); right. get(); // gotright chopstick view. set. Phil(identity, view. GOTRIGHT); sleep(500); left. get(); // eating view. set. Phil(identity, view. EATING); sleep(controller. eat. Time()); right. put(); left. put(); } } catch (java. lang. Interrupted. Exception e){} } } 0 1

Večerajúci filozofovia for (int i =0; i<N; ++i) fork[i] = new Fork(display, i); for

Večerajúci filozofovia for (int i =0; i<N; ++i) fork[i] = new Fork(display, i); for (int i =0; i<N; ++i){ phil[i] = new Philosopher (this, i, fork[(i-1+N)%N], fork[i]); phil[i]. start(); } Phil 0 thinking Phil 0 has Chopstick 0 Waiting for Chopstick 1 Phil 0 eating Phil 0 thinking Phil 0 has Chopstick 0 Waiting for Chopstick 1 Phil 0 eating Phil 0 thinking Phil 0 has Chopstick 0 Waiting for Chopstick 1 Phil 1 thinking Phil 2 thinking Phil 3 thinking Phil 4 thinking Phil 1 has Chopstick 1 Waiting for Chopstick 2 Phil 2 has Chopstick 2 Waiting for Chopstick 3 Phil 3 has Chopstick 3 Waiting for Chopstick 4 Phil 4 has Chopstick 4 Waiting for Chopstick 0

Ohraničený buffer public synchronized void put(Object o) throws Interrupted. Exception { while (count==size) wait();

Ohraničený buffer public synchronized void put(Object o) throws Interrupted. Exception { while (count==size) wait(); buf[in] = o; ++count; in=(in+1) % size; notify(); } public synchronized Object get() throws Interrupted. Exception { while (count==0) wait(); Object o =buf[out]; buf[out]=null; --count; out=(out+1) % size; notify(); return (o); }

Budík import java. util. Timer; import java. util. Timer. Task; • schedule(Timer. Task task,

Budík import java. util. Timer; import java. util. Timer. Task; • schedule(Timer. Task task, long delay, long period) • schedule(Timer. Task task, Date time, long period) • schedule. At. Fixed. Rate(Timer. Task task, long delay, long period) • schedule. At. Fixed. Rate(Timer. Task task, Date first. Time, long period) public class Reminder { Timer timer; public Reminder(int seconds) { timer = new Timer(); timer. schedule(new Remind. Task(), seconds*1000); } class Remind. Task extends Timer. Task { public void run() { System. out. println("Time's up!"); timer. cancel(); //Terminate thread } //Get the Date corresponding to 11: 00 pm today. } Calendar calendar = Calendar. get. Instance(); public static void main(String args[]) { calendar. set(Calendar. HOUR_OF_DAY, 23); new Reminder(5); calendar. set(Calendar. MINUTE, 1); System. out. println("Task scheduled. "); calendar. set(Calendar. SECOND, 0); } Date time = calendar. get. Time(); } timer = new Timer(); timer. schedule(new Remind. Task(), time);