新西兰服务器

java如何实现单线程启动多个线程处理一个列表并在所有子线程处理完毕后父线程再处理其他操作


java如何实现单线程启动多个线程处理一个列表并在所有子线程处理完毕后父线程再处理其他操作

发布时间:2021-12-18 16:34:17 来源:高防服务器网 阅读:96 作者:小新 栏目:编程语言

这篇文章将为大家详细讲解有关java如何实现单线程启动多个线程处理一个列表并在所有子线程处理完毕后父线程再处理其他操作,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

主类:ParentChildMain

父线程类:ParentChildPoolTest

子线程类:ParentChildDoThread

线程池:ThreadPool

import java.util.ArrayList;

public class ParentChildMain {
private static ArrayList list = new ArrayList();
private static int controlNum = 0;
private static int listNum = 0;
public ParentChildMain() {
 for (int i = 0; i < 50; i++) {
  list.add(String.valueOf(i));
 }
 listNum = list.size();
 ParentChildPoolTest test = new ParentChildPoolTest();
 test.start();
}

public static synchronized  String getControlObj(){
 if(listNum == controlNum){
  return null;
 }
 String retValue = (String)list.get(controlNum);
 controlNum ++;
 return retValue;
}

public static void main(String[] args){
 ParentChildMain main = new ParentChildMain();
}
}

import java.util.ArrayList;

public class ParentChildPoolTest extends Thread{

int numThreads = 10;
int numTasks = 4;
public ParentChildPoolTest(){
 
}
public void run(){
  // 生成线程池
       ThreadPool threadPool = new ThreadPool(numThreads);

// 运行任务
       for (int i=0; i<numTasks; i++) {
threadPool.runTask(new ParentChildDoThread());
       }

// 关闭线程池并等待所有任务完成
       threadPool.join();
       threadPool.close();

System.out.println("1111111111111111111111");
}

public static  void main(String[] args){
 
}

}

public class ParentChildDoThread extends Thread {

public void run() {
 String value = ParentChildMain.getControlObj();
 while ( value != null) {
  System.out.println("this is test:"+value);
  try {
   this.sleep(Integer.parseInt(value)*1000);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  value = ParentChildMain.getControlObj();
 }
}

}

/**
*

Title:

*

Description:

*

Copyright: Copyright (c) 2004

*

Company:

* @author not attributable
* @version 1.0
*/
import java.util.LinkedList;

/**
   线程池是一组线程,限制执行任务的线程数
*/
public class ThreadPool extends ThreadGroup {

private boolean isAlive;
 private LinkedList taskQueue;
 private int threadID;
 private static int threadPoolID;

/**
     创建新的线程池,numThreads是池中的线程数
  */
 public ThreadPool(int numThreads) {
   super("ThreadPool-" + (threadPoolID++));
   setDaemon(true);

isAlive = true;

taskQueue = new LinkedList();
   for (int i = 0; i < numThreads; i++) {
     new PooledThread().start();
   }
 }

/**
     请求新任务。任务在池中下一空闲线程中运行,任务按收到的顺序执行
  */
 public synchronized void runTask(Runnable task) {
   if (!isAlive) {
     throw new IllegalStateException(); //线程被关则抛出IllegalStateException异常
   }
   if (task != null) {
     taskQueue.add(task);
     notify();
   }

}

protected synchronized Runnable getTask() throws InterruptedException {
   while (taskQueue.size() == 0) {
     if (!isAlive) {
       return null;
     }
     wait();
   }
   return (Runnable) taskQueue.removeFirst();
 }

/**
     关闭线程池,所有线程停止,不再执行任务
  */
 public synchronized void close() {
   if (isAlive) {
     isAlive = false;
     taskQueue.clear();
     interrupt();
   }
 }

/**
     关闭线程池并等待所有线程完成,执行等待的任务
  */
 public void join() {
   //告诉等待线程线程池已关
   synchronized (this) {
     isAlive = false;
     notifyAll();
   }

// 等待所有线程完成
   Thread[] threads = new Thread[activeCount()];
   int count = enumerate(threads);
   for (int i = 0; i < count; i++) {
     try {
       threads[i].join();
     }
     catch (InterruptedException ex) {
       ex.printStackTrace();
     }
   }
 }

 /**
         用于进行任务的线程
  */
 private class PooledThread extends Thread {
   public PooledThread() {
     super(ThreadPool.this,"PooledThread-" + (threadID++));
   }

public void run() {
     while (!isInterrupted()) {
       // 得到任务
       Runnable task = null;
       try {
         task = getTask();
       }
       catch (InterruptedException ex) {
       }

// 若getTask()返回null或中断,则关闭此线程并返回
       if (task == null) {
         return;
       }

// 运行任务,吸收异常
       try {
         task.run();
       }
       catch (Throwable t) {
         uncaughtException(this, t);
       }
     }
   }
 }
}

关于“java如何实现单线程启动多个线程处理一个列表并在所有子线程处理完毕后父线程再处理其他操作”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

[微信提示:高防服务器能助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

[图文来源于网络,不代表本站立场,如有侵权,请联系高防服务器网删除]
[