Advertisement
Evilerus

w02_2

Apr 24th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. package tk.evilus;
  2.  
  3. public class w02_2 extends Thread {
  4.    
  5.     public void run() {
  6.         System.out.println("ID = "+this.getName());
  7.     }
  8.  
  9.     public static void main(String[] args) {
  10.         w02_2[] threads = new w02_2[101];
  11.        
  12.         for (int i=0; i<threads.length; i++) {
  13.             threads[i] = new w02_2();
  14.             threads[i].setName(Integer.toString(i));
  15.         }
  16.        
  17.         for (int i=0; i<threads.length; i+=2) {
  18.             threads[i].run();
  19.         }
  20.        
  21.         while (true) {
  22.             try {
  23.                 for (int i=0; i<threads.length; i+=2) {
  24.                     threads[i].join();
  25.                 }
  26.                 break;
  27.             } catch (Exception e) {
  28.                 e.printStackTrace(System.out);
  29.             }
  30.         }
  31.        
  32.         for (int i=1; i<threads.length; i+=2) {
  33.             threads[i].run();
  34.         }
  35.        
  36.         while (true) {
  37.             try {
  38.                 for (int i=1; i<threads.length; i+=2) {
  39.                     threads[i].join();
  40.                 }
  41.                 break;
  42.             } catch (Exception e) {
  43.                 e.printStackTrace(System.out);
  44.             }
  45.         }
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement