Advertisement
grzemot

PAI_lab1_zad1

Apr 1st, 2020
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1.  
  2. import static java.lang.Math.*;
  3. class MyThread extends Thread {
  4.     private int cnt;
  5.     private String str;
  6.     private int min=100;
  7.     private int max=999;
  8.     int range = (max - min) + 1;
  9.  
  10.     public MyThread(int c, String s){
  11.         cnt = c;
  12.         str = s;
  13.     }
  14.  
  15.     @Override public void run() {
  16.         try
  17.         {
  18.             for(int i =0; i <cnt; i++)
  19.             {
  20.                 System.out.println(str);
  21.                 Thread.sleep((long)(Math.random() * range) + min);
  22.             }
  23.         }
  24.         catch(InterruptedException ex)
  25.         {}
  26.  
  27.     }
  28. }
  29.  
  30.  
  31. public class Pingpong {
  32.  
  33.     public static void main(String[] args) throws InterruptedException {
  34.  
  35.         System.out.println("===== START ===========");
  36.         MyThread t1 = new MyThread(10, "Ping");
  37.         MyThread t2 = new MyThread(10, "Pong");
  38.         MyThread t3 = new MyThread(10, "PENG");
  39.  
  40.         t1.start();
  41.         t2.start();
  42.         t3.start();
  43.  
  44.         t1.join();
  45.         t2.join();
  46.         t3.join();
  47.  
  48.         System.out.println("===== STOP ===========");
  49.  
  50.     }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement