/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun * Microsystems, Inc. All Rights Reserved. */ /** * Does JVM implementation supports background threads by yielding? * * @author Petr Kuzel */ public class JVMPrioritiesTest { /** Creates a new instance of JVMPrioritiesTest */ public JVMPrioritiesTest() { } /** * @param args the command line arguments */ public static void main(String[] args) throws Exception { final long start = System.currentTimeMillis(); class Sprint implements Runnable { private String name; boolean back = false; Sprint(String name, boolean back) { this.name = name; this.back = back; } public void run() { StringBuffer cpuWork = new StringBuffer(500000); synchronized (JVMPrioritiesTest.class) { System.err.println(name + " started."); } for (int i = 0; i<100000; i++) { cpuWork.append(i % 17); for (int j = 0; j<1; j++) { cpuWork.append(cpuWork.hashCode() % 2); cpuWork.deleteCharAt(cpuWork.hashCode() % 2); } if (back) Thread.yield(); } synchronized (JVMPrioritiesTest.class) { System.err.println(name + " finished in " + (System.currentTimeMillis() - start) + "ms."); } } }; // HotSpot new Sprint("InitHostSpot", false).run(); Thread background = new Thread(new Sprint("Background", true), "Background"); Thread foreground = new Thread(new Sprint("Foreground", false), "Foreground"); synchronized (JVMPrioritiesTest.class) { background.setPriority(Thread.MIN_PRIORITY); foreground.setPriority(Thread.MAX_PRIORITY); foreground.start(); background.start(); System.err.print("Starter: prepare, "); Thread.sleep(1000); // let them allocate System.err.println("start!"); } // PIII@800 // Linux 2.4.20 // InitHostSpot started. // InitHostSpot finished in 26786ms. // Starter: prepare, start! // Foreground started. // Background started. // Foreground finished in 63179ms. // Background finished in 97969ms. } }