java多线程

package javabasis;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 项目名称:svn
 * 类名称:MoreThread
 * 类描述:线程池
 * 创建人:qian.long
 * 创建时间:2011-9-13 上午09:57:28
 * 修改人:qian.long
 * 修改时间:2011-9-13 上午09:57:28
 * 修改备注:
 * @version
 */
public class MoreThread {
	public static void main(String[] args) {

		BlockingQueue<Runnable> bqueue = new ArrayBlockingQueue<Runnable>(20);
		ThreadPoolExecutor pool = new ThreadPoolExecutor(20, 1000, 50, TimeUnit.MILLISECONDS, bqueue); 
		Thread t1 = new Mytyread();
		Thread t2 = new Mytyread();
		Thread t3 = new Mytyread();
		Thread t4 = new Mytyread();
		Thread t5 = new Mytyread();
		Thread t6 = new Mytyread();
		Thread t7 = new Mytyread();
		//将线程放入池中进行执行 
		pool.execute(t1);
		pool.execute(t2);
		pool.execute(t3);
		pool.execute(t4);
		pool.execute(t5);
		pool.execute(t6);
		pool.execute(t7);
		//关闭线程池 
		pool.shutdown();
	}

	public static void test() {
		//创建等待队列 
		BlockingQueue<Runnable> bqueue = new ArrayBlockingQueue<Runnable>(20); 
		//创建一个单线程执行程序,它可安排在给定延迟后运行命令或者定期地执行。 
		ThreadPoolExecutor pool = new ThreadPoolExecutor(20, 1000, 50, TimeUnit.MILLISECONDS, bqueue); 
		//创建实现了Runnable接口对象,Thread对象当然也实现了Runnable接口 
		Thread t1 = new MyThread();
		Thread t2 = new MyThread();
		Thread t3 = new MyThread();
		Thread t4 = new MyThread();
		Thread t5 = new MyThread();
		Thread t6 = new MyThread();
		Thread t7 = new MyThread();
		//将线程放入池中进行执行 
		pool.execute(t1);
		pool.execute(t2);
		pool.execute(t3);
		pool.execute(t4);
		pool.execute(t5);
		pool.execute(t6);
		pool.execute(t7);
		//关闭线程池 
		pool.shutdown();
	}
}

class Mytyread extends Thread {
	@Override
	public void run() {
		System.out.println(Thread.currentThread().getName() + ":MoreThread.test()正在进行中.....");
		MoreThread.test();
		try {
			Thread.sleep(100L);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

class MyThread extends Thread {
	@Override
	public void run() {
		System.out.println(Thread.currentThread().getName() + "正在进行中.....");
		if("pool-4-thread-7".equals(Thread.currentThread().getName())) {
			try {
				throw new Exception("------");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try {
			Thread.sleep(100L);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

//用给定的初始参数和默认的线程工厂及处理程序创建新的 ThreadPoolExecutor。
//使用 Executors 工厂方法之一比使用此通用构造方法方便得多。
//参数:
//corePoolSize - 池中所保存的线程数,包括空闲线程。
//maximumPoolSize - 池中允许的最大线程数。
//keepAliveTime - 当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间。
//unit - keepAliveTime 参数的时间单位。
//workQueue - 执行前用于保持任务的队列。此队列仅保持由 execute 方法提交的 Runnable 任务。
//抛出:
//IllegalArgumentException - 如果 corePoolSize 或 keepAliveTime 小于零,
//或者 maximumPoolSize 小于或等于零,或者 corePoolSize 大于 maximumPoolSize。
//NullPointerException - 如果 workQueue 为 null
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值