preempt.java

来自「kaffe Java 解释器语言,源码,Java的子集系统,开放源代码」· Java 代码 · 共 48 行

JAVA
48
字号
public class Preempt implements Runnable{    public final static int TIMEOUT = 2000;    public static void main(String args[])    {	new Thread() {	    public void run() {		try {		    Thread.sleep(TIMEOUT);		} catch (InterruptedException _) {		    ;		}		System.exit(0);	    }	}.start();	for (int i = 0; i < 5; i++)	    new Thread(new Preempt(), "thread " + i).start();	synchronized(Preempt.class) {	    System.out.println("started 5 threads");	}    }    public void run()    {	// this shouldn't be necessary---System.out.println should be	// synchronized, but it apparently is (9/30/99)	synchronized(Preempt.class) {	    System.out.println(Thread.currentThread().getName());	}	while (true) {		continue;	}    }}// Sort output/* Expected Output:started 5 threadsthread 0thread 1thread 2thread 3thread 4*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?