Created
October 22, 2014 12:32
-
-
Save autermann/d45f6ac4ae36426c2994 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Clown {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// run as 'java -Djava.security.manager Main' | |
public class Main { | |
public static void main(String[] args) { | |
Volkswagen vw = new Volkswagen(); | |
for (int i = 0; i < 20; ++i) { | |
vw.add(new Clown()); | |
} | |
vw.done(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashSet; | |
import java.util.Set; | |
public final class Volkswagen { | |
private static final int CAPACITY = 5; | |
private final Set<Clown> clowns = new HashSet<>(); | |
public Volkswagen add(Clown clown) { | |
synchronized(this) { | |
if (clowns.size() >= CAPACITY) { | |
throw new IllegalStateException("I'm full"); | |
} else { | |
clowns.add(clown); | |
} | |
return this; | |
} | |
} | |
public Volkswagen done() { | |
synchronized(this) { | |
if (clowns.size() == 20) { | |
System.out.println("I'm a Volkswagen with 20 clowns!"); | |
} else { | |
System.out.println("Way to much space in here!"); | |
} | |
return this; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment