多线程模拟记者发布会。每个记者提一个问题,且发布会要等每个记者问题准备好,会议才能开始,待所有记者发问完后,会议结束。
下面是模拟实例:
测试结果:
-----Metting is start----
狗仔2号的问题是:开了2几瓶茅台,够不够喝?
狗仔5号的问题是:开了5几瓶茅台,够不够喝?
狗仔6号的问题是:开了6几瓶茅台,够不够喝?
狗仔3号的问题是:开了3几瓶茅台,够不够喝?
狗仔8号的问题是:开了8几瓶茅台,够不够喝?
狗仔4号的问题是:开了4几瓶茅台,够不够喝?
狗仔1号的问题是:开了1几瓶茅台,够不够喝?
狗仔7号的问题是:开了7几瓶茅台,够不够喝?
狗仔10号的问题是:开了10几瓶茅台,够不够喝?
狗仔9号的问题是:开了9几瓶茅台,够不够喝?
-----Metting is end----
下面是模拟实例:
package cn.com.ld.study.thread;
/**
* @filename: Reporter
* @description: 记者
* @author lida
* @date 2013-3-29 下午1:35:01
*/
public class Reporter {
private String name;
private String question;
Reporter(String name, String question) {
this.name = name;
this.question = question;
}
public void ask() {
System.out.println(name + "的问题是:" + this.question);
}
public String getName() {
return name;
}
}
package cn.com.ld.study.thread;
import java.util.concurrent.CountDownLatch;
/**
* @filename: AskJob
* @description: 提问线程
* @author lida
* @date 2013-3-29 下午1:34:42
*/
public class AskJob extends Thread {
private Reporter reporter;
private CountDownLatch startMetting;
private CountDownLatch endMetting;
AskJob(Reporter reporter, CountDownLatch startMetting,
CountDownLatch endMetting) {
this.reporter = reporter;
this.startMetting = startMetting;
this.endMetting = endMetting;
}
public void run() {
try {
sleep(2000);
startMetting.await();
reporter.ask();
endMetting.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
}
package cn.com.ld.study.thread;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
/**
* @filename: ReporterMetting
* @description: 会议现场
* @author lida
* @date 2013-3-29 下午1:35:20
*/
public class ReporterMetting extends Thread {
private int reporeterNo;
private CountDownLatch startMetting;
private CountDownLatch endMetting;
private List<Reporter> repList;
ReporterMetting(int reporeterNo, List<Reporter> repList,
CountDownLatch start) {
this.reporeterNo = repList.size();
this.repList = repList;
this.startMetting = start;
this.endMetting = new CountDownLatch(reporeterNo);
}
public void run() {
System.out.println("-----Metting is start----");
AskJob aj = null;
for (int i = 0; i < this.reporeterNo; i++) {
Reporter reporter = repList.get(i);
aj = new AskJob(reporter, startMetting, endMetting);
aj.start();
}
// 待所有的问题准备就绪后,会议真正的开始
startMetting.countDown();
try {
// 等待askjob 全部执行完毕唤醒 结束会议通知
endMetting.await();
System.out.println("-----Metting is end----");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
List<Reporter> repList = new ArrayList<Reporter>();
int reporeterNo = 10;
Reporter reporter;
for (int i = 1; i <= reporeterNo; i++) {
reporter = new Reporter("狗仔" + i + "号", "开了" + i + "几瓶茅台,够不够喝?");
repList.add(reporter);
}
CountDownLatch startMetting = new CountDownLatch(1);
ReporterMetting rm = new ReporterMetting(reporeterNo, repList,
startMetting);
rm.start();
}
}
测试结果:
-----Metting is start----
狗仔2号的问题是:开了2几瓶茅台,够不够喝?
狗仔5号的问题是:开了5几瓶茅台,够不够喝?
狗仔6号的问题是:开了6几瓶茅台,够不够喝?
狗仔3号的问题是:开了3几瓶茅台,够不够喝?
狗仔8号的问题是:开了8几瓶茅台,够不够喝?
狗仔4号的问题是:开了4几瓶茅台,够不够喝?
狗仔1号的问题是:开了1几瓶茅台,够不够喝?
狗仔7号的问题是:开了7几瓶茅台,够不够喝?
狗仔10号的问题是:开了10几瓶茅台,够不够喝?
狗仔9号的问题是:开了9几瓶茅台,够不够喝?
-----Metting is end----