java 线程实践倒计时,倒计时多线程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.");

}

}

解决方案

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

public class Service {

private List 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();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值