• 线程池小程序(Java)


    //固定大小的线程池
    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();
    	}
    	}
    	
    	
    }
    
    //本来该看分词器 路线有点错了 回头!!
  • 相关阅读:
    [AGC030F] Permutation and Minimum
    nginx
    Flex建立AS项目时,如何设定发布的舞台大小
    让Flex 支持VSS
    Flex编程实用技巧
    Flash/Flex学习笔记(57):实用技巧
    sql 2000 "无法执行查询,因为一些文件缺少或未注册"的解决办法
    Flash/Flex学习笔记(56):矩阵变换
    什么是反向代理,如何利用反向代理提高网站性能
    AS3及Flex的百条常用知识
  • 原文地址:https://www.cnblogs.com/lixingle/p/3313041.html
Copyright © 2020-2023  润新知