Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1. package lista5nowa;
  2.  
  3. class Watek extends Thread {
  4.     int id;
  5.  
  6.     public Watek(int id) {
  7.         this.id = id;
  8.     }
  9.  
  10.     public void run() {
  11.         // System.out.println("Watek " + id);
  12.         System.out.println(this.getName());
  13.         try {
  14.             sleep(1000);
  15.         } catch (InterruptedException e) {
  16.             // TODO Auto-generated catch block
  17.             e.printStackTrace();
  18.         }
  19.     }
  20.  
  21. }
  22.  
  23. public class zad {
  24.  
  25.     public static void main(String[] args) {
  26.  
  27.         final int N = 20;
  28.  
  29.         Watek[] threads = new Watek[N];
  30.         for (int i = 0; i < N; i++) {
  31.             threads[i] = new Watek(i);
  32.  
  33.         }
  34.         int i;
  35.         for (i = 0; i < N; i++) {
  36.             if (i % 2 == 0) {
  37.                 threads[i].start();
  38.                 try {
  39.                     threads[i].join();
  40.                 } catch (InterruptedException e) {
  41.                     // TODO Auto-generated catch block
  42.                     e.printStackTrace();
  43.                 }
  44.             }
  45.         }
  46.         for (i = 0; i < N; i++) {
  47.             if (i % 2 != 0) {
  48.                 threads[i].start();
  49.                 try {
  50.                     threads[i].join();
  51.                 } catch (InterruptedException e) {
  52.                     // TODO Auto-generated catch block
  53.                     e.printStackTrace();
  54.                 }
  55.             }
  56.         }
  57.     }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement