IT-Academy

Nahoda Vlakna

Aug 14th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.65 KB | None | 0 0
  1.  
  2. import java.util.Random;
  3.  
  4. /**
  5.  *
  6.  * @author IT Academy
  7.  */
  8. public class Nahoda implements Runnable {
  9.  
  10.     String meno;
  11.     int cas;
  12.     Random r = new Random();
  13.  
  14.     public Nahoda(String meno) {
  15.         this.meno = meno;
  16.         cas = r.nextInt(9999); // medzi 0-1 sekundami
  17.     }
  18.  
  19.     // Tato metoda bezi ked zacnu pracovat thready
  20.     @Override
  21.     public void run() {
  22.         try {
  23.             System.out.printf("%s vlakno bude spat %d milisekund\n", meno, cas);
  24.             Thread.sleep(cas); // Ako dlho chces spat
  25.             System.out.printf("%s je ok\n", meno);
  26.         } catch (InterruptedException e) {
  27.         }
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment