Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

multithreading - countdown multiple threads java

I have 4 threads witch are printing numbers from 15 to 0.I want to control executing of my threads for example I want first to thread D to finish and after him thread C and after him thread B and finally thread A. For now they are doing it all parallel. How can I change that? any suggestions?

Here is my code:

// 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.");
   }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you should design structure of you service class first. I can suggest following:

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();
} 

Next problem is to implement start and awaitStart (stop methods implemented similar). I recommend to use tools from java.util.concurrent for implementing awaitStart method. E.g. CountDownLatch. Each service has it's own latch that indicates that server is started. So code for awaitStart and start is following:

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...