无法确定一个活着的线程是可运行还是被阻塞了;也无法确定一个可运行的线程是否在运行;另外,你也无法区分死亡线程和非可运行线程。这句话如何理解?

解决方案 »

  1.   

    就是线程很难判断它的状态,你必须给它上锁,不然会乱套,
    你好好看看,理解,运行一下我下面的代码就应该可以理解了:public class Sample08 extends Thread {
    private Object o1 = null;
    private Object o2 = null;
    public Sample08(Object o1, Object o2) {
    this.o1 = o1;
    this.o2 = o2;
    }
    public void run() {
    synchronized (o1) {
    System.out.println(Thread.currentThread().getName() + "锁住对象" + o1);

    try {
    Thread.sleep(100);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    synchronized (o2) {
    System.out.println(Thread.currentThread().getName() + "锁住对象" + o2);
    }
    System.out.println(Thread.currentThread().getName() + "释放对象" + o2);
    }
    System.out.println(Thread.currentThread().getName() + "释放对象" + o1);
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
    String str1 = "str1";
    String str2 = "str2";
    String str3 = "str3";

    Sample08 t1 = new Sample08(str1, str2);
    Sample08 t2 = new Sample08(str2, str3);
    Sample08 t3 = new Sample08(str3, str1);

    t1.start();
    t2.start();
    t3.start();
    }
    }