package pub.capart; /*** This is a simple class designed to simulate an application consuming* CPU, memory, and contending for a synchronization lock.*/public class Grinderv1 { private static Grinderv1 singleton = new Grinderv1(); private static final String randstr = "this is just a random string that I'm going to add up many many times"; public static Grinderv1 getGrinder() { return singleton; } public synchronized void grindCPU(int level) { StringBuffer sb = new StringBuffer(); String s = randstr; for (int i=0;i<level;++i) { sb.append(s); s = getReverse(sb.toString()); } } public String getReverse(String s) { StringBuffer sb = new StringBuffer(s); sb = sb.reverse(); return sb.toString(); }}
package pub.capart; /*** This is a simple class designed to simulate an application consuming* CPU, memory, and contending for a synchronization lock.*/public class Grinderv2 { private static Grinderv2 singleton = new Grinderv2(); private static final String randstr = "this is just a random string that I'm going to add up many many times"; private StringBuffer sbuf = new StringBuffer(); private StringBuffer sbufrev = new StringBuffer(); public static Grinderv2 getGrinder() { return singleton; } public synchronized void grindCPU(int level) { sbufrev.setLength(0); sbufrev.append(randstr); sbuf.setLength(0); for (int i=0;i<level;++i) { sbuf.append(sbufrev); reverse(); } return sbuf.toString(); } public String getReverse(String s) { StringBuffer sb = new StringBuffer(s); sb = sb.reverse(); return sb.toString(); }}
package pub.capart;/*** This is just a dummy class designed to simulate a process consuming* CPU, memory, and contending for a synchronization lock.*/public class Grinderv3 { private static Grinderv3 grinders[]; private static int grinderRoundRobin = 0; private static final String randstr = "this is just a random string that I'm going to add up many many times"; private StringBuffer sbuf = new StringBuffer(); private StringBuffer sbufrev = new StringBuffer(); static { grinders = new Grinderv3[10]; for (int i=0;i<grinders.length;++i) { grinders[i] = new Grinderv3(); } } public synchronized static Grinderv3 getGrinder() { Grinderv3 g = grinders[grinderRoundRobin]; grinderRoundRobin = (grinderRoundRobin +1) % grinders.length; return g; } public synchronized void grindCPU(int level) { sbufrev.setLength(0); sbufrev.append(randstr); sbuf.setLength(0); for (int i=0;i<level;++i) { sbuf.append(sbufrev); reverse(); } return sbuf.toString(); } public String getReverse(String s) { StringBuffer sb = new StringBuffer(s); sb = sb.reverse(); return sb.toString(); }}