Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- How to run
- $ javac Sheep.java && java Sheep -Xms1k -Xmx1k -XX:MaxPermSize=1k -XX:MaxNewSize=1k
- Sample output
- 양(id=35994) is created
- 양(id=35995) is created
- 양(id=35996) is created
- 양(id=17679) is killed, current alive 양 14956마리
- 양(id=35997) is created
- 양(id=17678) is killed, current alive 양 14967마리
- 양(id=17677) is killed, current alive 양 14967마리
- 양(id=17676) is killed, current alive 양 14966마리
- 양(id=17675) is killed, current alive 양 14965마리
- */
- class Sheep {
- private static long nextId = 0;
- private static long aliveCounter = 0;
- private long id;
- public Sheep() {
- aliveCounter += 1;
- nextId += 1;
- this.id = nextId;
- String msg = String.format("양(id=%d) is created", this.id);
- System.out.println(msg);
- }
- @Override
- protected void finalize() throws Throwable {
- aliveCounter -= 1;
- String msg = String.format("양(id=%d) is killed, current alive 양 %d마리", this.id, aliveCounter);
- System.out.println(msg);
- super.finalize();
- }
- public static void main(String[] args) {
- for(int i = 0 ; i < 100000 ; ++i) {
- new Sheep();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement