Created
July 16, 2016 13:19
-
-
Save fab1an/3632f51e6704c87c138a987cd8347cfb 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
package finalizertest; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import android.support.test.runner.AndroidJUnit4; | |
@RunWith(AndroidJUnit4.class) | |
public class FinalizerTest { | |
private final Logger L = LoggerFactory.getLogger(FinalizerTest.class); | |
@Test | |
public void test1() { | |
testNormal(); | |
testWithTrivialFinalizer(); | |
testWithComplexFinalizer(); | |
} | |
@Test | |
public void test2() { | |
testNormal(); | |
testWithComplexFinalizer(); | |
testWithTrivialFinalizer(); | |
} | |
@Test | |
public void test3() { | |
testWithTrivialFinalizer(); | |
testNormal(); | |
testWithComplexFinalizer(); | |
} | |
@Test | |
public void test4() { | |
testWithTrivialFinalizer(); | |
testWithComplexFinalizer(); | |
testNormal(); | |
} | |
@Test | |
public void test5() { | |
testWithComplexFinalizer(); | |
testNormal(); | |
testWithTrivialFinalizer(); | |
} | |
@Test | |
public void test6() { | |
testWithComplexFinalizer(); | |
testWithTrivialFinalizer(); | |
testNormal(); | |
} | |
private void testNormal() { | |
long myTime = System.nanoTime(); | |
for (int i=0; i<1000; i++) { | |
new Obj(i); | |
} | |
long took = System.nanoTime() - myTime; | |
L.info("normal took {} ms", took / 1000); | |
} | |
private void testWithTrivialFinalizer() { | |
long myTime = System.nanoTime(); | |
for (int i=0; i<1000; i++) { | |
new ObjWithTrivialFinalizer(i); | |
} | |
long took = System.nanoTime() - myTime; | |
L.info("trivial finalizer took {} ms", took / 1000); | |
} | |
private void testWithComplexFinalizer() { | |
long myTime = System.nanoTime(); | |
for (int i=0; i<1000; i++) { | |
new ObjWithComplexFinalizer(i); | |
} | |
long took = System.nanoTime() - myTime; | |
L.info("complex finalizer took {} ms", took / 1000); | |
} | |
public static class Obj { | |
private int num; | |
public Obj(final int num) { | |
this.num = num; | |
} | |
} | |
public static class ObjWithTrivialFinalizer { | |
private int num; | |
public ObjWithTrivialFinalizer(final int num) { | |
this.num = num; | |
} | |
@Override | |
protected void finalize() throws Throwable { | |
super.finalize(); | |
} | |
} | |
public static class ObjWithComplexFinalizer { | |
private int num; | |
public ObjWithComplexFinalizer(final int num) { | |
this.num = num; | |
} | |
@Override | |
protected void finalize() throws Throwable { | |
try { | |
if (this.num < 0) { | |
/* never happens */ | |
throw new IllegalStateException("something"); | |
} | |
} finally { | |
super.finalize(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment