⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threadstate.java

📁 kaffe Java 解释器语言,源码,Java的子集系统,开放源代码
💻 JAVA
字号:
import java.util.*;import java.io.*;public class ThreadState extends Thread {  public final static int DEFAULT_NUMTHREADS = 10;  boolean childRunning;  static PrintStream p;  // Test thread states  public static void main(String args[]) throws Exception {    int numThreads;    int index = 0;    if (args.length > 0 && args[0].equals("-v")) {      p = System.out;      index = 1;    }    try {      numThreads = Integer.parseInt(args[index]);    } catch (Exception e) {      numThreads = DEFAULT_NUMTHREADS;    }	new Thread() {	    public void run() {		try {		    Thread.sleep(60 * 1000);		} catch (Exception _) { }		System.out.println("Time out.  Failure.");		System.exit(-1);	    }	}.start();    Thread[] threads = new Thread[numThreads];    for (int i = 0; i < numThreads; i++) {      threads[i] = new ThreadState();      verbose("main starting " + threads[i].getName());      threads[i].start();    }    for (int i = 0; i < numThreads; i++) {      try {	verbose("main joining " + threads[i].getName());	threads[i].join();      } catch (InterruptedException e) {	check(false, "main " + e);      }    }    System.exit(0);  }  public void run() {    verbose(getName() + " running");    // Create child thread    Thread t = new Thread() {      public synchronized void run() {	childRunning = true;	verbose(ThreadState.this.getName() + " child running [child]");	try {	  this.wait(0);	} catch (InterruptedException e) {	  check(false, "thread " + e);	}	verbose(ThreadState.this.getName() + " child thread exiting");      }    };    // Check state    check(!t.isAlive(), "alive before start()");    verbose(getName() + " starting child thread");    t.start();    check(t.isAlive(), "dead after start()");    // Check setDaemon after start()    try {      t.setDaemon(false);      check(false, "setDaemon() after start");    } catch (IllegalThreadStateException e) {    }    // Check double start()    try {      t.start();      check(false, "start() while alive");    } catch (IllegalThreadStateException e) {    }    // Wait for thread to be running    while (!childRunning) {      Thread.yield();    }    verbose(getName() + " child thread running [parent]");    // Notify child he can exit    synchronized (t) {      t.notify();    }    verbose(getName() + " joining my child thread");    try {      t.join();    } catch (InterruptedException e) {      check(false, "join: " + e);    }    check(!t.isAlive(), "alive after join()");    // Check double start()    try {      t.start();      check(false, "start() after dead");    } catch (IllegalThreadStateException e) {    }    // OK    synchronized (ThreadState.class) {      System.out.println("Success.");    }    verbose(getName() + " exiting");  }  public static void check(boolean that, String msg) {    if (!that) {      System.err.println("Failure: " + msg);      System.exit(1);    }  }  public static void verbose(String msg) {    if (p != null) {      p.println(msg);      p.flush();    }  }}/* Expected Output:Success.Success.Success.Success.Success.Success.Success.Success.Success.Success.*/

⌨️ 快捷键说明

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