• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

线程池小程序(Java)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
//固定大小的线程池
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class ThreadTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//创建一个可重用固定线程数的线程池
		ExecutorService pool=Executors.newFixedThreadPool(2);
		//创建实现了Runnable接口对象,Thread的对象相应也实现了Runnable接口
		Thread t1=new MyThread();
		Thread t2=new MyThread();
		Thread t3=new MyThread();
		Thread t4=new MyThread();
		Thread t5=new MyThread();
		//将线程放入池中进行执行
		pool.execute(t1);
		pool.execute(t2);
		pool.execute(t3);
		pool.execute(t4);
		pool.execute(t5);
		//关闭线程池
		pool.shutdown();
		
	}

}
 class MyThread extends Thread
{
	public void run()
	{
		System.out.println(Thread.currentThread().getName()+"····正在运行");
	}
	
}
//创建一个使用单个worker线程的Executor与可重用固定个数线程数的线程池只改了一行代码
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class ThreadTest2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//创建一个可重用单个线程数的线程池
		ExecutorService pool=Executors.newSingleThreadExecutor();
		//创建实现了Runnable接口对象,Thread的对象相应也实现了Runnable接口
		Thread t1=new MyThread2();
		Thread t2=new MyThread2();
		Thread t3=new MyThread2();
		Thread t4=new MyThread2();
		Thread t5=new MyThread2();
		//将线程放入池中进行执行
		pool.execute(t1);
		pool.execute(t2);
		pool.execute(t3);
		pool.execute(t4);
		pool.execute(t5);
		//关闭线程池
		pool.shutdown();
		
	}

}
 class MyThread2 extends Thread
{
	public void run()
	{
		System.out.println(Thread.currentThread().getName()+"····正在运行");
	}
	
}
//创建一个可变尺寸的 线程池只改一下pool的创建方式即可
//创建一个可根据需要创建新线程的线程池但是在以前构造的的线程可用时将重用他们
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class ThreadTest3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//创建一个可重用单个线程数的线程池
		ExecutorService pool=Executors.newCachedThreadPool();
		//创建实现了Runnable接口对象,Thread的对象相应也实现了Runnable接口
		Thread t1=new MyThread4();
		Thread t2=new MyThread4();
		Thread t3=new MyThread4();
		Thread t4=new MyThread4();
		Thread t5=new MyThread4();
		//将线程放入池中进行执行
		pool.execute(t1);
		pool.execute(t2);
		pool.execute(t3);
		pool.execute(t4);
		pool.execute(t5);
		//关闭线程池
		pool.shutdown();
		
	}

}
 class MyThread3 extends Thread
{
	public void run()
	{
		System.out.println(Thread.currentThread().getName()+"····正在运行");
	}
	
}
//自定义 线程池

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


public class ThreadTest4 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//创建等待队列
		BlockingQueue bqueue=new ArrayBlockingQueue(20);
		//创建一个单个线程数的线程池,他可安排在延迟完成后运行命令或者定期的执行
		ThreadPoolExecutor pool=new ThreadPoolExecutor(2, 3, 2, TimeUnit.MILLISECONDS, bqueue);
		//创建实现了Runnable接口对象,Thread的对象相应也实现了Runnable接口
		Thread t1=new MyThread4();
		Thread t2=new MyThread4();
		Thread t3=new MyThread4();
		Thread t4=new MyThread4();
		Thread t5=new MyThread4();
		Thread t6=new MyThread4();
		Thread t7=new MyThread4();
		//将线程放入池中进行执行
		pool.execute(t1);
		pool.execute(t2);
		pool.execute(t3);
		pool.execute(t4);
		pool.execute(t5);
		pool.execute(t6);
		pool.execute(t7);
		//关闭线程池
		pool.shutdown();
		
	}

}
 class MyThread4 extends Thread
{
	public void run()
	{
		System.out.println(Thread.currentThread().getName()+"····正在运行");
		try 
	{
		Thread.sleep(100l);
	}
	catch (InterruptedException e)
	{
		e.printStackTrace();
	}
	}
	
	
}
//本来该看分词器 路线有点错了 回头!!

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
20171128微信小程序发布时间:2022-07-18
下一篇:
微信小程序开发的那些坑,你踩过吗?发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap