本帖最后由 sl514 于 2010-07-07 10:03:50 编辑

解决方案 »

  1.   

    while(a.isAlive||b.isAlive||c.isAlive||d.isAlive){
      sleep(1000);
    }
    e.start();
      

  2.   

    给前四个线程设置标识位。
    e线程wait
    判断前四个线程的标示如果都true则notify
      

  3.   

    可以使用线程的join方法,你将e线程作为主线程,然后abcd在主线程里面join
      

  4.   


    public class JoinThread extends Thread {    public static int n = 0;    static synchronized void inc() {
            n++;
        }    public void run() {
            for (int i = 0; i < 10; i++)
                try {
                    inc();
                    sleep(3); // 为了使运行结果更随机,延迟3毫秒
                } catch (Exception e) {            }
        }    public static void main(String[] args) throws Exception {
            Thread threads[] = new Thread[100];
            for (int i = 0; i < threads.length; i++)
                // 建立100个线程
                threads[i] = new JoinThread();
            for (int i = 0; i < threads.length; i++)
                // 运行刚才建立的100个线程
                threads[i].start();
            if (args.length > 0)
                for (int i = 0; i < threads.length; i++)
                    // 100个线程都执行完后继续
                    threads[i].join();
            System.out.println("n=" + JoinThread.n);
        }}
      

  5.   


    public class TestThread {
    private boolean falg = false;
    public TestThread() {
    }
    public void task1() {
    synchronized (this) {
    while (!falg) {
    try {
    System.out.println("wait ....");
    this.wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    System.out.println("I'm E");
    }
    }
    public void task2() {
    synchronized (this) {
    System.out.println("Do other");
    falg=true;
    this.notifyAll();
    }
    }
    }
    public class ThreadDemo1  extends Thread {
    private TestThread tt;
    public ThreadDemo1(TestThread tt){
    this.tt=tt;
    }
    public void run() {
    tt.task1();
    }
    }
    public class ThreadDemo2  extends Thread {
    private TestThread tt;
    public ThreadDemo2(TestThread tt){
    this.tt=tt;
    }
    public void run() {
    tt.task2();
    }
    }
    public class Test {
    public static void main(String[] args){
    TestThread tt=new TestThread();
    new ThreadDemo1(tt).start();  
    new ThreadDemo2(tt).start();  
    }
    }