這篇文章將為大家詳細講解有關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如何實現單線程啟動多個線程處理一個列表并在所有子線程處理完畢后父線程再處理其他操作”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。