Advertisement
kajacx

concurrency

Sep 14th, 2013
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1. package test;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8.  
  9. public class ThreadTest {
  10.  
  11.     public static void main(String[] args) {
  12.         Share commonObj = new Share();
  13.         Thread[] threads = ModuloIncrement.createThreads(commonObj, 2);
  14.  
  15.         for (Thread t : threads) {
  16.             t.start();
  17.         }
  18.  
  19.         for (Thread t : threads) {
  20.             try {
  21.                 t.join();
  22.             } catch (InterruptedException ex) {
  23.                 Logger.getLogger(ThreadTest.class.getName()).log(Level.SEVERE, null, ex);
  24.             }
  25.         }
  26.  
  27.         System.out.println("Exit Main Thread");
  28.  
  29.     }
  30. }
  31.  
  32. class ModuloIncrement implements Runnable {
  33.  
  34.     private final Share share;
  35.     private final int base, modulo;
  36.  
  37.     private ModuloIncrement(Share share, int base, int modulo) {
  38.         this.share = share;
  39.         this.base = base;
  40.         this.modulo = modulo;
  41.     }
  42.  
  43.     public void run() {
  44.         print("start");
  45.  
  46.         synchronized (share) {
  47.             while (true) {
  48.                 if (share.number >= share.max) {
  49.                     break;
  50.                 }
  51.                 if (share.number % base == modulo) {
  52.                     print(share.number + "");
  53.                     share.notifyAll();
  54.                     share.number++;
  55.                 } else {
  56.                     try {
  57.                         share.wait();
  58.                     } catch (InterruptedException ex) {
  59.                         Logger.getLogger(ModuloIncrement.class.getName()).log(Level.SEVERE, null, ex);
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.  
  65.         print("end");
  66.     }
  67.  
  68.     private void print(String msg) {
  69.         System.out.println(this + ": " + msg);
  70.     }
  71.  
  72.     public static Thread[] createThreads(Share share, int count) {
  73.         Thread[] mit = new Thread[count];
  74.         for (int i = 0; i < count; i++) {
  75.             mit[i] = new Thread(new ModuloIncrement(share, count, i));
  76.         }
  77.         List<Thread> l = Arrays.asList(mit);
  78.         Collections.shuffle(l);
  79.         return l.toArray(mit);
  80.     }
  81.  
  82.     @Override
  83.     public String toString() {
  84.         return "ModuloIncrement{" + "share=" + share + ", base=" + base + ", modulo=" + modulo + '}';
  85.     }
  86. }
  87.  
  88. class Share {
  89.  
  90.     volatile int number = 1;
  91.     final int max = 50;
  92.  
  93.     @Override
  94.     public String toString() {
  95.         return "Share{" + "number=" + number + ", max=" + max + '}';
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement