這篇文章將為大家詳細(xì)講解有關(guān)java如何實(shí)現(xiàn)單線程啟動(dòng)多個(gè)線程處理一個(gè)列表并在所有子線程處理完畢后父線程再處理其他操作,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)公司是一家專業(yè)從事網(wǎng)站設(shè)計(jì)、做網(wǎng)站的網(wǎng)絡(luò)公司。作為專業(yè)網(wǎng)絡(luò)公司,成都創(chuàng)新互聯(lián)公司依托的技術(shù)實(shí)力、以及多年的網(wǎng)站運(yùn)營(yíng)經(jīng)驗(yàn),為您提供專業(yè)的成都網(wǎng)站建設(shè)、全網(wǎng)整合營(yíng)銷推廣及網(wǎng)站設(shè)計(jì)開發(fā)服務(wù)!
主類: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);
// 運(yùn)行任務(wù)
for (int i=0; i
}
// 關(guān)閉線程池并等待所有任務(wù)完成
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;
/**
線程池是一組線程,限制執(zhí)行任務(wù)的線程數(shù)
*/
public class ThreadPool extends ThreadGroup {
private boolean isAlive;
private LinkedList taskQueue;
private int threadID;
private static int threadPoolID;
/**
創(chuàng)建新的線程池,numThreads是池中的線程數(shù)
*/
public ThreadPool(int numThreads) {
super("ThreadPool-" + (threadPoolID++));
setDaemon(true);
isAlive = true;
taskQueue = new LinkedList();
for (int i = 0; i < numThreads; i++) {
new PooledThread().start();
}
}
/**
請(qǐng)求新任務(wù)。任務(wù)在池中下一空閑線程中運(yùn)行,任務(wù)按收到的順序執(zhí)行
*/
public synchronized void runTask(Runnable task) {
if (!isAlive) {
throw new IllegalStateException(); //線程被關(guān)則拋出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();
}
/**
關(guān)閉線程池,所有線程停止,不再執(zhí)行任務(wù)
*/
public synchronized void close() {
if (isAlive) {
isAlive = false;
taskQueue.clear();
interrupt();
}
}
/**
關(guān)閉線程池并等待所有線程完成,執(zhí)行等待的任務(wù)
*/
public void join() {
//告訴等待線程線程池已關(guān)
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();
}
}
}
/**
用于進(jìn)行任務(wù)的線程
*/
private class PooledThread extends Thread {
public PooledThread() {
super(ThreadPool.this,"PooledThread-" + (threadID++));
}
public void run() {
while (!isInterrupted()) {
// 得到任務(wù)
Runnable task = null;
try {
task = getTask();
}
catch (InterruptedException ex) {
}
// 若getTask()返回null或中斷,則關(guān)閉此線程并返回
if (task == null) {
return;
}
// 運(yùn)行任務(wù),吸收異常
try {
task.run();
}
catch (Throwable t) {
uncaughtException(this, t);
}
}
}
}
}
關(guān)于“java如何實(shí)現(xiàn)單線程啟動(dòng)多個(gè)線程處理一個(gè)列表并在所有子線程處理完畢后父線程再處理其他操作”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。