zoukankan      html  css  js  c++  java
  • java学习之线程池的实现

    package com.gh.threadpoor;
    
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    /**
     * 使用线程池
     * @author ganhang
     *
     */
    public class ThreadPoorDemo {
        public static void main(String[] args) {
            //创建一个单线程的线程池 
            ExecutorService es=Executors.newSingleThreadExecutor();
            //创建一个固定大小的线程池(2个)
            ExecutorService es1=Executors.newFixedThreadPool(2);
            //创建一个可缓存的线程池,保存线程60s再回收
            ExecutorService es2=Executors.newCachedThreadPool();
            //创建一个无限制大小的线程池,需要给定一个起始大小(最小的大小),但不会回收
            ExecutorService es3=Executors.newScheduledThreadPool(2);
            Mythread m1=new Mythread(); 
            Mythread m2=new Mythread(); 
            es.execute(m1); 
            es.execute(m2);
            
        }
    }
    class Mythread implements Runnable{
        @Override
        public void run() {
            for(int i=0;i<10;i++){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"--"+i);
            }
        }
        
    }
  • 相关阅读:
    [LeetCode]Reverse Linked List II
    [LeetCode]Move Zeroes
    Next Greater Element I
    Keyboard Row
    Number Complement
    SQL语句学习(二)
    SQL语句学习(一)
    jQuery学习(三)
    jQuery学习(二)
    JQuery学习(一)
  • 原文地址:https://www.cnblogs.com/ganhang-acm/p/5154362.html
Copyright © 2011-2022 走看看