标签:
问题背景:
/**
* 子线程类
* @author Administrator
*
*/
public class SubThread implements Runnable{
private int status = 99; //99-初始化 0-执行成功 1-执行失败
public void run() {
System.out.println("开始执行...");
try{
Thread.sleep(2000);
}catch(Exception e){
e.printStackTrace();
}
status=0;
System.out.println("执行完毕...");
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
} |
/**
* 主线程类
* @author Administrator
*
*/
public class MainThread {
private List<SubThread> subThreadList = new ArrayList<SubThread>();
/**
* 创建子线程
* @return
*/
public SubThread createSubThread(){
SubThread subThread = new SubThread();
subThreadList.add(new SubThread());
return subThread;
}
public boolean start(){
for(SubThread subThread : subThreadList){
new Thread(subThread).start();
}
/**
* 监控所有子线程是否执行完毕
*/
boolean continueFlag = true;
while(continueFlag){
for(SubThread subThread : subThreadList){
if(subThread.getStatus()==99){
continueFlag = true;
break;
}
continueFlag = false;
}
}
/**
* 判断子线程的执行结果
*/
boolean result = true;
for(SubThread subThread : subThreadList){
if(subThread.getStatus()!=0){
result = false;
break;
}
}
return result;
}
|
public static void main(String[] args) {
MainThread main = new MainThread();
main.createSubThread();
main.createSubThread();
main.createSubThread();
boolean result = main.start();
System.out.println(result);
}
|
/**
* 计数器类
* @author Administrator
*
*/
public class CountLauncher {
private int count;
public CountLauncher(int count){
this.count = count;
}
public synchronized void countDown(){
count --;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
} |
/**
* 子线程类
* @author Administrator
*
*/
public class SubThread implements Runnable{
/**
* 计数器类对象实例
*/
private CountLauncher countLauncher;
private int status = 99; //99-初始化 0-执行成功 1-执行失败
public void run() {
System.out.println("开始执行...");
try{
Thread.sleep(2000);
}catch(Exception e){
e.printStackTrace();
}
status=0;
System.out.println("执行完毕...");
countLauncher.countDown();
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public CountLauncher getCountLauncher() {
return countLauncher;
}
public void setCountLauncher(CountLauncher countLauncher) {
this.countLauncher = countLauncher;
}
|
/**
* 主线程类
* @author Administrator
*
*/
public class MainThread {
private List<SubThread> subThreadList = new ArrayList<SubThread>();
/**
* 创建子线程
* @return
*/
public synchronized SubThread createSubThread(){
SubThread subThread = new SubThread();
subThreadList.add(new SubThread());
return subThread;
}
public boolean start(){
CountLauncher countLauncher = new CountLauncher(subThreadList.size());
for(SubThread subThread : subThreadList){
subThread.setCountLauncher(countLauncher);
new Thread(subThread).start();
}
while(countLauncher.getCount()>0){
System.out.println(countLauncher.getCount());
}
/**
* 判断子线程的执行结果
*/
boolean result = true;
for(SubThread subThread : subThreadList){
if(subThread.getStatus()!=0){
result = false;
break;
}
}
return result;
}
/**
* 测试实例
*/
public static void main(String[] args) {
MainThread main = new MainThread();
main.createSubThread();
main.createSubThread();
main.createSubThread();
boolean result = main.start();
System.out.println(result);
}
|
/**
* 子线程
* @author Administrator
*
*/
public class SubThread implements Runnable{
private int status =99;
private CountDownLatch threadsSignal;
@Override
public void run(){
System.out.println("开始执行...");
try{
Thread.sleep(2000);
}catch(Exception e){
e.printStackTrace();
}
status=0;
System.out.println("执行完毕...");
threadsSignal.countDown();
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public CountDownLatch getThreadsSignal() {
return threadsSignal;
}
public void setThreadsSignal(CountDownLatch threadsSignal) {
this.threadsSignal = threadsSignal;
}
} |
public class MainThread {
public List<SubThread> subThreadList = new ArrayList<SubThread>();
/**
* 创建子线程
* @param clazzPath
* @param methodName
* @return
*/
public SubThread createSubThread(){
SubThread subThread = new SubThread();
subThreadList.add(subThread);
return subThread;
}
/**
* 启动线程
*/
public int start() throws Exception{
CountDownLatch threadSignal = new CountDownLatch(subThreadList.size());
for(SubThread subThread :subThreadList){
subThread.setThreadsSignal(threadSignal);
Thread thread = new Thread(subThread);
thread.start();
}
threadSignal.await();
System.out.println("所有子线程执行完毕...");
for(SubThread subThread :subThreadList){
if(subThread.getStatus()!=0){
return 1;
}
}
return 0;
}
public static void main(String[] args) throws Exception{
MainThread main = new MainThread();
main.createSubThread();
main.createSubThread();
main.createSubThread();
main.createSubThread();
int result = main.start();
System.out.println("执行结果:" + result);
}
|
标签:
原文地址:http://www.cnblogs.com/itmanxgl/p/a0fdc9caa310823318defda84fafba82.html