0

我有 4 个线程,女巫正在打印从 15 到 0 的数字。我想控制我的线程的执行,例如,我希望首先线程 D 完成,然后线程 C,然后线程 B,最后线程 A。现在它们是并行进行。我该如何改变呢?有什么建议么?

这是我的代码:

// Suspending and resuming a thread for Java 2
class NewThread implements Runnable {
   String name; // name of thread
   Thread t;
   boolean suspendFlag;
   NewThread(String threadname) {
      name = threadname;
      t = new Thread(this, name);
      System.out.println("New thread: " + t);
      suspendFlag = false;
      t.start(); // Start the thread
   }
   // This is the entry point for thread.
   public void run() {
      try {
      for(int i = 15; i > 0; i--) {
         System.out.println(name + ": " + i);
         Thread.sleep(200);
         synchronized(this) {
            while(suspendFlag) {
               wait();
            }
          }
        }
      } catch (InterruptedException e) {
         System.out.println(name + " interrupted.");
      }
      System.out.println(name + " exiting.");
   }
   void mysuspend() {
      suspendFlag = true;
   }
   synchronized void myresume() {
      suspendFlag = false;
       notify();
   }
}

public class SuspendResume {
   public static void main(String args[]) {
      NewThread A = new NewThread("A");
      NewThread B = new NewThread("B");
      NewThread C = new NewThread("C");
      NewThread D = new NewThread("D");
//      try {
//        System.out.println("****************************************************************");
//        System.out.println(A.t.getState());
//        System.out.println(B.t.getState());
//        System.out.println(C.t.getState());
//        System.out.println(D.t.getState());
//        
//        if(D.t.isAlive())
//        {
//            System.out.println("Bla bla bla");
//        }
//            
//        Thread.sleep(1000);
//        A.mysuspend();
//        System.out.println("Suspending thread One");
//        Thread.sleep(1000);
//         A.myresume();
//         System.out.println("Resuming thread One");
//         B.mysuspend();
//         System.out.println("Suspending thread Two");
//         Thread.sleep(1000);
//         B.myresume();
//         System.out.println("Resuming thread Two");
//        
//         
//        
//      } catch (InterruptedException e) {
//         System.out.println("Main thread Interrupted");
//      }
      // wait for threads to finish
      try {
         System.out.println("Waiting for threads to finish.");
         A.t.join();
         B.t.join();
         C.t.join();
         D.t.join();
      } catch (InterruptedException e) {
         System.out.println("Main thread Interrupted");
      }
      System.out.println("Main thread exiting.");
   }
}
4

1 回答 1

0

我认为您应该首先设计服务类的结构。我可以建议以下:

public class Service {

    private List<Service> dependencies;

    // Starts service. 
    // It should wait until all dependencies started using awaitStart method and then start itself
    public void start();

    // Blocks current thread until service is started. 
    // If it is started returns immediately.
    public void awaitStart();

    // Stops service.
    // Awaits until all dependencies are stopped using awaitStop.
    public void stop();

    // Blocks current thread until service is stopped.
    // If it is already stops returns immediately
    public void awaitStop();

    // Actual code that has service specific code.
    // This method may be invoked as last line in 'start' method.  
    public void run();
} 

下一个问题是实现startawaitStartstop方法实现类似)。我建议使用来自java.util.concurrent实现awaitStart方法的工具。例如CountDownLatch。每个服务都有自己的闩锁,表明服务器已启动。所以代码awaitStartstart如下:

private CountDownLatch started = new CountDownLatch(1);

public void awaitStart() {
    started.await();
}

public void start() {
    for (Service service : dependencies) {
        service.awaitStart();
    }
    System.out.println("Service " + name + " is started");
    started.countDown();
    run();
}
于 2013-06-13T15:30:38.230 回答