溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》
  • 首頁 > 
  • 教程 > 
  • 開發技術 > 
  • 編程語言 > 
  • java如何實現單線程啟動多個線程處理一個列表并在所有子線程處理完畢后父線程再處理其他操作

java如何實現單線程啟動多個線程處理一個列表并在所有子線程處理完畢后父線程再處理其他操作

發布時間:2021-12-18 16:34:17 來源:億速云 閱讀:276 作者:小新 欄目:編程語言

這篇文章將為大家詳細講解有關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如何實現單線程啟動多個線程處理一個列表并在所有子線程處理完畢后父線程再處理其他操作”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女