Last active
August 21, 2019 20:59
-
-
Save chathurawidanage/f09fdfee2feebb1c19441a6f05f297ce to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.cwidanage; | |
import java.util.LinkedList; | |
import java.util.Queue; | |
public class KryoTest { | |
public static Queue<A> queue = new LinkedList<>(); | |
static class A { | |
@Override | |
protected void finalize() throws Throwable { | |
// control how much we pool | |
if (queue.size() < 1000) { | |
queue.add(this); | |
} | |
} | |
} | |
static A instance() { | |
if (queue.isEmpty()) { | |
System.out.println("New"); | |
return new A(); | |
} | |
System.out.println("Reuse"); | |
return queue.poll(); | |
} | |
public static void main(String[] args) { | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
while (true) { | |
System.gc(); | |
try { | |
Thread.sleep(10000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
}); | |
while (true) { | |
instance(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment