Java 实例 - 查看线程是否存活
以下实例演示了如何通过继承 Thread 类并使用 isAlive() 方法来检测一个线程是否存活:
public class TwoThreadAlive extends Thread { public void run() { for (int i = 0; i < 10; i++) { printMsg(); } } public void printMsg() { Thread t = Thread.currentThread(); String name = t.getName(); System.out.println("name=" + name); } public static void main(String[] args) { TwoThreadAlive tt = new TwoThreadAlive(); tt.setName("Thread"); System.out.println("before start(), tt.isAlive()=" + tt.isAlive()); tt.start(); System.out.println("just after start(), tt.isAlive()=" + tt.isAlive()); for (int i = 0; i < 10; i++) { tt.printMsg(); } System.out.println("The end of main(), tt.isAlive()=" + tt.isAlive()); } }
以上代码运行输出结果为:
before start(), tt.isAlive()=false just after start(), tt.isAlive()=true name=main name=Thread name=main name=main name=main name=main name=main name=main name=main name=main name=main name=Thread name=Thread name=Thread name=Thread name=Thread The end of main(), tt.isAlive()=true name=Thread name=Thread name=Thread name=Thread
Java 实例 - 获取当前线程名称
以下实例演示了如何通过继承 Thread 类并使用 getName() 方法来获取当前线程名称:
public class TwoThreadGetName extends Thread { public void run() { for (int i = 0; i < 10; i++) { printMsg(); } } public void printMsg() { Thread t = Thread.currentThread(); String name = t.getName(); System.out.println("name=" + name); } public static void main(String[] args) { TwoThreadGetName tt = new TwoThreadGetName(); tt.start(); for (int i = 0; i < 10; i++) { tt.printMsg(); } } }
以上代码运行输出结果为:
name=main name=main name=main name=main name=main name=Thread-0 name=Thread-0 name=Thread-0 name=Thread-0 name=Thread-0 name=main name=Thread-0 name=main name=Thread-0 name=main name=Thread-0 name=main name=Thread-0 name=main name=Thread-0
Java 实例 - 状态监测
以下实例演示了如何通过继承 Thread 类并使用 currentThread.getName() 方法来监测线程的状态:
class MyThread extends Thread{ boolean waiting= true; boolean ready= false; MyThread() { } public void run() { String thrdName = Thread.currentThread().getName(); System.out.println(thrdName + " starting."); while(waiting) System.out.println("waiting:"+waiting); System.out.println("waiting..."); startWait(); try { Thread.sleep(1000); } catch(Exception exc) { System.out.println(thrdName + " interrupted."); } System.out.println(thrdName + " terminating."); } synchronized void startWait() { try { while(!ready) wait(); } catch(InterruptedException exc) { System.out.println("wait() interrupted"); } } synchronized void notice() { ready = true; notify(); } } public class Main { public static void main(String args[]) throws Exception{ MyThread thrd = new MyThread(); thrd.setName("MyThread #1"); showThreadStatus(thrd); thrd.start(); Thread.sleep(50); showThreadStatus(thrd); thrd.waiting = false; Thread.sleep(50); showThreadStatus(thrd); thrd.notice(); Thread.sleep(50); showThreadStatus(thrd); while(thrd.isAlive()) System.out.println("alive"); showThreadStatus(thrd); } static void showThreadStatus(Thread thrd) { System.out.println(thrd.getName() + "Alive:=" + thrd.isAlive() + " State:=" + thrd.getState()); } }
以上代码运行输出结果为:
…… alive alive MyThread #1 terminating. alive ……
Java 实例 - 线程优先级设置
以下实例演示了如何通过setPriority() 方法来设置线程的优先级:
public class SimplePriorities extends Thread { private int countDown = 5; private volatile double d = 0; public SimplePriorities(int priority) { setPriority(priority); start(); } public String toString() { return super.toString() + ": " + countDown; } public void run() { while(true) { for(int i = 1; i < 100000; i++) d = d + (Math.PI + Math.E) / (double)i; System.out.println(this); if(--countDown == 0) return; } } public static void main(String[] args) { new SimplePriorities(Thread.MAX_PRIORITY); for(int i = 0; i < 5; i++) new SimplePriorities(Thread.MIN_PRIORITY); } }
以上代码运行输出结果为:
Thread[Thread-1,1,main]: 5 Thread[Thread-2,1,main]: 5 Thread[Thread-3,1,main]: 5 Thread[Thread-0,10,main]: 5 Thread[Thread-3,1,main]: 4 Thread[Thread-0,10,main]: 4 Thread[Thread-1,1,main]: 4 Thread[Thread-5,1,main]: 5 Thread[Thread-4,1,main]: 5 Thread[Thread-2,1,main]: 4 Thread[Thread-0,10,main]: 3 Thread[Thread-1,1,main]: 3 Thread[Thread-4,1,main]: 4 Thread[Thread-2,1,main]: 3 ……