Skip to content

Instantly share code, notes, and snippets.

@autermann
Created October 22, 2014 12:32
Show Gist options
  • Save autermann/d45f6ac4ae36426c2994 to your computer and use it in GitHub Desktop.
Save autermann/d45f6ac4ae36426c2994 to your computer and use it in GitHub Desktop.
public class Clown {}
// 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();
}
}
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