java 事务管理 子父线程_java 单线程(暂且理解为父线程)启动多个线程处理(理解为子线程)处理一个列表,并在所有子线程处理完毕后父线程再处理其他操作...

主类:ParentChildMain

父线程类:ParentChildPoolTest

子线程类:ParentChildDoThread

线程池:ThreadPool[@more@]

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 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);

}

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值