import java.util.*; import java.util.zip.*; import java.util.List; import java.util.regex.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; import javax.swing.table.*; import java.io.*; import java.net.*; import java.lang.reflect.*; import java.lang.ref.*; import java.lang.management.*; import java.security.*; import java.security.spec.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import java.math.*; public class main { public static void main(String[] args) throws Exception { Thread t = new Thread(new Runnable() { public void run() { try { while (licensed()) { limitCPU(10); } } catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); } } }, "Burn 10%"); t.start(); while (licensed()) { sleepSeconds(1); limitCPU_Data data = limitCPU_data.get(t); print(data.sleepTime + " / " + data.calcTime + " = " + formatDouble(((double) data.calcTime)/data.sleepTime, 2)); } } static volatile StringBuffer local_log = new StringBuffer(); // not redirected static volatile StringBuffer print_log = local_log; // might be redirected, e.g. to main bot // in bytes - will cut to half that static volatile int print_log_max = 1024*1024; static volatile int local_log_max = 100*1024; static boolean print_silent; // total mute if set static void print() { print(""); } // slightly overblown signature to return original object... static A print(A o) { if (print_silent) return o; String s = String.valueOf(o) + "\n"; StringBuffer loc = local_log; StringBuffer buf = print_log; int loc_max = print_log_max; if (buf != loc && buf != null) { print_append(buf, s, print_log_max); loc_max = local_log_max; } if (loc != null) print_append(loc, s, loc_max); System.out.print(s); return o; } static void print(long l) { print(String.valueOf(l)); } static void print(char c) { print(String.valueOf(c)); } static void print_append(StringBuffer buf, String s, int max) { synchronized(buf) { buf.append(s); max /= 2; if (buf.length() > max) try { int newLength = max/2; int ofs = buf.length()-newLength; String newString = buf.substring(ofs); buf.setLength(0); buf.append("[...] ").append(newString); } catch (Exception e) { buf.setLength(0); } } } public static String formatDouble(double d, int digits) { String format = "0."; for (int i = 0; i < digits; i++) format += "#"; return new java.text.DecimalFormat(format, new java.text.DecimalFormatSymbols(Locale.ENGLISH)).format(d); } static void sleepSeconds(double s) { if (s > 0) sleep(round(s*1000)); } static class limitCPU_Data { int granularity = 10; double percentage; volatile long calcingSince; volatile long sleepTime, calcTime; } //static ThreadLocal limitCPU_data = new ThreadLocal(); static Map limitCPU_data = synchroMap(new WeakHashMap()); static void limitCPU(double percentage) { limitCPU_Data data = limitCPU_data.get(currentThread()); if (data == null) limitCPU_data.put(currentThread(), data = new limitCPU_Data()); if (ping()) data.calcingSince = 0; //limitCPU_Data data = limitCPU_data.get(); if (data.percentage != percentage) { data.percentage = percentage; data.calcTime = data.sleepTime = 0; } if (data.calcingSince == 0) { data.calcingSince = now(); return; } long newCalcTime = now() - data.calcingSince; if (newCalcTime >= data.granularity) { // don't try to sleep too often data.calcTime += newCalcTime; double ratio = percentage/100; double allowedCalcTime = data.sleepTime*ratio/(1-ratio); long sleep = toLong(max(0, data.calcTime-allowedCalcTime)); if (sleep > 0) { sleep(sleep); data.sleepTime += sleep; } data.calcingSince = now(); } } static void sleep(long ms) { try { Thread.sleep(ms); } catch (Exception e) { throw new RuntimeException(e); } } static void sleep() { try { print("Sleeping."); synchronized(main.class) { main.class.wait(); } } catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }} static Map synchroMap() { return synchroHashMap(); } static Map synchroMap(Map map) { return Collections.synchronizedMap(map); } static Thread currentThread() { return Thread.currentThread(); } static long round(double d) { return Math.round(d); } static long toLong(Object o) { if (o instanceof Number) return ((Number) o).longValue(); if (o instanceof String) return parseLong((String) o); return 0; } static int max(int a, int b) { return Math.max(a, b); } static long max(int a, long b) { return Math.max((long) a, b); } static long max(long a, long b) { return Math.max(a, b); } static double max(int a, double b) { return Math.max((double) a, b); } static int max(Collection c) { int x = Integer.MIN_VALUE; for (int i : c) x = max(x, i); return x; } static double max(double[] c) { if (c.length == 0) return Double.MIN_VALUE; double x = c[0]; for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]); return x; } static byte max(byte[] c) { byte x = -128; for (byte d : c) if (d > x) x = d; return x; } static volatile boolean ping_pauseAll; static int ping_sleep = 100; // poll flag every 100 // returns true if it did anything static boolean ping() { if (ping_pauseAll) { do sleep(ping_sleep); while (ping_pauseAll); return true; } return false; } static long now_virtualTime; static long now() { return now_virtualTime != 0 ? now_virtualTime : System.currentTimeMillis(); } static long parseLong(String s) { if (s == null) return 0; return Long.parseLong(dropSuffix("L", s)); } static long parseLong(Object s) { return Long.parseLong((String) s); } static Map synchroHashMap() { return Collections.synchronizedMap(new HashMap()); } static String dropSuffix(String suffix, String s) { return s.endsWith(suffix) ? s.substring(0, l(s)-l(suffix)) : s; } static int l(Object[] array) { return array == null ? 0 : array.length; } static int l(byte[] array) { return array == null ? 0 : array.length; } static int l(int[] array) { return array == null ? 0 : array.length; } static int l(char[] array) { return array == null ? 0 : array.length; } static int l(Collection c) { return c == null ? 0 : c.size(); } static int l(Map m) { return m == null ? 0 : m.size(); } static int l(String s) { return s == null ? 0 : s.length(); } static int l(Object o) { return l((List) o); // incomplete } static volatile boolean licensed_yes = true; static boolean licensed() { return licensed_yes; } static void licensed_off() { licensed_yes = false; } }