Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package you;
- import clowns.Clown;
- import clowns.Volkswagen;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * @author Sebastien Lorber <i>(lorber.sebastien@gmail.com)</i>
- */
- public class You {
- private static void sleep(int millies) {
- try {
- Thread.sleep(millies);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
- public static final Volkswagen vw = new Volkswagen();
- public static int clownsInHashCode = 0;
- public static void main(String args[]) {
- List<Clown> clownsList = new ArrayList<Clown>();
- for (int i = 0; i < 20; i++) {
- final int clownIndex = i;
- Clown clown = new Clown() {
- @Override
- public int hashCode() {
- clownsInHashCode++;
- while ( clownsInHashCode != 20 ) {
- synchronized ( vw ) {
- sleep(100);
- try {
- vw.wait();
- } catch (InterruptedException e) { }
- sleep(100);
- vw.notifyAll();
- }
- }
- synchronized ( vw ) {
- vw.notifyAll();
- }
- return super.hashCode();
- }
- };
- clownsList.add(clown);
- }
- for ( Clown clown : clownsList ) {
- final Clown tempClown = clown;
- Thread thread = new Thread( new Runnable() {
- public void run() {
- synchronized ( vw ) {
- System.out.println("Starting new thread");
- vw.add( tempClown );
- }
- }
- });
- thread.start();
- }
- while ( clownsInHashCode != 20 ) {
- System.out.println("Only " + clownsInHashCode + " clowns waiting in the add method, need to wait a little bit more");
- sleep(100);
- }
- System.out.println("All clown are actually inside the Set.add method but the set size is not incremented yet");
- vw.done();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement