Advertisement
Guest User

Untitled

a guest
Feb 17th, 2012
934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.31 KB | None | 0 0
  1. package you;
  2.  
  3. import clowns.Clown;
  4. import clowns.Volkswagen;
  5.  
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9.  
  10. /**
  11.  * @author Sebastien Lorber <i>(lorber.sebastien@gmail.com)</i>
  12.  */
  13. public class You {
  14.  
  15.  
  16.     private static void sleep(int millies) {
  17.         try {
  18.             Thread.sleep(millies);
  19.         } catch (InterruptedException e) {
  20.             throw new RuntimeException(e);
  21.         }
  22.     }
  23.  
  24.  
  25.     public static final Volkswagen vw = new Volkswagen();
  26.  
  27.     public static int clownsInHashCode = 0;
  28.  
  29.  
  30.     public static void main(String args[]) {
  31.  
  32.         List<Clown> clownsList = new ArrayList<Clown>();
  33.  
  34.         for (int i = 0; i < 20; i++) {
  35.             final int clownIndex = i;
  36.             Clown clown =  new Clown() {
  37.                 @Override
  38.                 public int hashCode() {
  39.                     clownsInHashCode++;
  40.                     while ( clownsInHashCode != 20 ) {
  41.                         synchronized ( vw ) {
  42.                             sleep(100);
  43.                             try {
  44.                                 vw.wait();
  45.                             } catch (InterruptedException e) {  }
  46.                             sleep(100);
  47.                             vw.notifyAll();
  48.                         }
  49.                     }
  50.                     synchronized ( vw ) {
  51.                         vw.notifyAll();
  52.                     }
  53.                     return super.hashCode();
  54.                 }
  55.             };
  56.             clownsList.add(clown);
  57.         }
  58.  
  59.  
  60.         for ( Clown clown : clownsList ) {
  61.             final Clown tempClown = clown;
  62.             Thread thread = new Thread( new Runnable() {
  63.                 public void run() {
  64.                     synchronized ( vw ) {
  65.                         System.out.println("Starting new thread");
  66.                         vw.add( tempClown );
  67.                     }
  68.                 }
  69.             });
  70.             thread.start();
  71.         }
  72.  
  73.        
  74.         while ( clownsInHashCode != 20 ) {
  75.             System.out.println("Only " + clownsInHashCode + " clowns waiting in the add method, need to wait a little bit more");
  76.             sleep(100);
  77.         }
  78.         System.out.println("All clown are actually inside the Set.add method but the set size is not incremented yet");
  79.         vw.done();
  80.     }
  81.    
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement