资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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

这篇文章将为大家详细讲解有关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; ithreadPool.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如何实现单线程启动多个线程处理一个列表并在所有子线程处理完毕后父线程再处理其他操作”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


文章名称:java如何实现单线程启动多个线程处理一个列表并在所有子线程处理完毕后父线程再处理其他操作
URL标题:http://cdkjz.cn/article/jogodj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220