From 4adeb3d441ffe9bbc452e2f3e1fc03052c8f0323 Mon Sep 17 00:00:00 2001 From: Chris Newland Date: Wed, 14 Jan 2015 23:35:42 +0000 Subject: [PATCH] Tweaks to PolymorphismTest Sandbox example --- .../resources/examples/PolymorphismTest.java | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/main/resources/examples/PolymorphismTest.java b/src/main/resources/examples/PolymorphismTest.java index 2a092d44..9da2b3cd 100644 --- a/src/main/resources/examples/PolymorphismTest.java +++ b/src/main/resources/examples/PolymorphismTest.java @@ -5,21 +5,20 @@ public interface Animal void speak(); } + public static int woofs = 0, miaows = 0, moos = 0; + public class Dog implements Animal { - public int woofs = 0; public void speak() { woofs++; } } public class Cat implements Animal { - public int miaows = 0; public void speak() { miaows++; } } public class Cow implements Animal { - public int moos = 0; public void speak() { moos++; } } @@ -31,12 +30,9 @@ public PolymorphismTest() Animal creature = null; - // Run with -XX:-TieredCompilation and -XX:-Inline - // to see the effect of HotSpot optimising the virtual call - - // 1 = monomorphic dispatch - virtual call will be optimised - // 2 = bimorphic dispatch - virtual call will be optimised - // 3 = polymorphic dispatch - virtual call will not be optimised + // change the variable maxImplementations to control the inlining behaviour + // 2 = bimorphic dispatch - the method call will be inlined + // 3 = polymorphic dispatch - the method call will not be inlined final int maxImplementations = 2; @@ -51,11 +47,12 @@ public PolymorphismTest() creature.speak(); } + + System.out.println("Woofs:" + woofs + " Miaows:" + miaows + " Moos:" + moos); } public static void main(String[] args) { new PolymorphismTest(); } - } \ No newline at end of file