Skip to content

Instantly share code, notes, and snippets.

@chathurawidanage
Last active August 21, 2019 20:59
Show Gist options
  • Save chathurawidanage/f09fdfee2feebb1c19441a6f05f297ce to your computer and use it in GitHub Desktop.
Save chathurawidanage/f09fdfee2feebb1c19441a6f05f297ce to your computer and use it in GitHub Desktop.
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