wait()方法 线程进入等待池中
synchronized void notice() throws InterruptedException {
// ready=true;
// notify();
Thread r=new Thread();
r.wait();
}
notifyAll()方法(唤醒所有 wait 线程)或 notify()方法(只随机唤醒一个 wait 线程)
synchronized void notice() { // ready=true; // notify(); Thread r=new Thread(); r.notify(); }
状态检测:Thread.getState()
package learning; class ThreadState extends Thread{ boolean waiting=true; boolean ready=false; ThreadState(){ } public void run(){ String thrname=Thread.currentThread().getName(); System.out.println(thrname+"starting"); while(waiting){ System.out.println("waiting:"+waiting); System.out.println("waiting"); startWait(); try{ Thread.sleep(1000); }catch (Exception e){ System.out.println(thrname+"interrupted"); } System.out.println(thrname+"terminating"); } } synchronized void startWait(){ try{ while (!ready) { System.out.println("waiting place "+ready); wait(); } }catch(InterruptedException e){ System.out.println("wait() interrupted"); } } synchronized void notice() { ready=true; notify(); } } class Main{ public static void main(String []arg) throws InterruptedException { ThreadState thrd=new ThreadState(); 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()); } }
setPriority()
public class priority extends Thread{ private int countDown=5; private volatile double d=0; public priority(int p){ setPriority(p); 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 []arg){ new priority(Thread.MAX_PRIORITY); for(int i=0;i<5;i++){ new priority(Thread.MIN_PRIORITY); } } }
join() 的作用:让“主线程”等待“子线程”结束之后才能继续运行。
public class SleepingThread extends Thread{ private int countDown=5; private static int threadCount=0; public SleepingThread(){ super(""+ ++threadCount); start(); } public String toString(){ return "#"+getName()+": "+countDown; } public void run(){ while(true){ System.out.println(this); if(--countDown==0){ return; } try { sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String []arg) throws InterruptedException { for(int i=0;i<5;i++){ new SleepingThread().join(); System.out.println("线程已被挂起"); } } }
new SleepingThread()在SleepingThread中创建,要等run结束之后才能挂起
使用interrupt方法来终端线程可分为两种情况:
-
线程处于阻塞状态,如使用了sleep方法。
-
使用while(!isInterrupted()){……}来判断线程是否被中断。
在第一种情况下使用interrupt方法,sleep方法将抛出一个InterruptedException例外,而在第二种情况下线程将直接退出。下面的代码演示了在第一种情况下使用interrupt方法。
package learning; import java.io.IOException; public class ThreadInterrupt extends Thread{ public void run(){ try { sleep(50000); } catch (InterruptedException e) { System.out.println(e.getMessage()); } } public static void main(String []arg) throws InterruptedException, IOException { Thread thread=new ThreadInterrupt(); thread.start(); // System.out.println("按键"); // System.in.read(); thread.interrupt(); thread.join(); System.out.println("线程已退出"); } }
执行run时候遇到sleep抛出异常中断