倒计时多线程java [英] countdown multiple threads java

查看:74
本文介绍了倒计时多线程java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 4 个线程正在打印从 15 到 0 的数字.我想控制我的线程的执行,例如我想先线程 D 完成,然后是线程 C,在他之后是线程 B,最后是线程 A.对于现在他们正在做这一切.我怎样才能改变它?有什么建议么?

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?

这是我的代码:

// 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.");
   }
}

推荐答案

我认为你应该先设计你的服务类的结构.我可以提出以下建议:

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

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

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

这篇关于倒计时多线程java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆