多线程之线程池
多线程之线程池
kimyeongnam 发表于8个月前
多线程之线程池
  • 发表于 8个月前
  • 阅读 20
  • 收藏 1
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

摘要: 简单介绍java4种线程池的使用

Java通过Executors提供四种线程池,分别为:newCachedThreadPool、newFixedThreadPool、newScheduledThreadPool、newSingleThreadExecutor

使用线程池首先要有多线程,下面是一个简单的买票系统多线程。

class TicketWidow implements Runnable{
    private int ticketNum = 0;  
    public TicketWidow(){
        this.ticketNum = 5;
    }
    public TicketWidow(int ticketNum) {  
        this.ticketNum = ticketNum;  
    }  
    public int getTicketNum() {  
        return ticketNum;  
    }  
    public void setTicketNum(int ticketNum) {  
        this.ticketNum = ticketNum;  
    }  
    public synchronized void sellOneTicket(String window){  
        if(this.ticketNum > 0){  
            System.out.println(window + "售出一张票,剩余票数: " + --this.ticketNum);  
        }else{  
            System.out.println("车票已售罄!");  
        }  
    }
    @Override
    public void run() {
        while(getTicketNum() > 0){  
            sellOneTicket(Thread.currentThread().getName());  
        }  
    }
}


1.newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。实例如下

public class ThreadTest {
    public static void main(String [] args) {
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();  
        TicketWidow mWindow = new TicketWidow(5);
        cachedThreadPool.execute(new Thread(mWindow));
        cachedThreadPool.execute(new Thread(mWindow));
    }
}

//执行结果
pool-1-thread-2售出一张票,剩余票数: 4
pool-1-thread-2售出一张票,剩余票数: 3
pool-1-thread-1售出一张票,剩余票数: 2
pool-1-thread-2售出一张票,剩余票数: 1
pool-1-thread-2售出一张票,剩余票数: 0
车票已售罄!


2.newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。实例如下

public class ThreadTest {
    public static void main(String [] args) {
        ExecutorService fixedThreadPool  = Executors.newFixedThreadPool(3);  
        TicketWidow mWindow = new TicketWidow(5);
        fixedThreadPool.execute(new Thread(mWindow));
        fixedThreadPool.execute(new Thread(mWindow));
        fixedThreadPool.execute(new Thread(mWindow));
        fixedThreadPool.execute(new Thread(mWindow));
    }
}

//执行结果
pool-1-thread-3售出一张票,剩余票数: 4
pool-1-thread-2售出一张票,剩余票数: 3
pool-1-thread-3售出一张票,剩余票数: 2
pool-1-thread-1售出一张票,剩余票数: 1
pool-1-thread-1售出一张票,剩余票数: 0
车票已售罄!

3.newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。

public class ThreadTest {
    public static void main(String [] args) {
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(2);  
        TicketWidow mWindow = new TicketWidow(5);
        scheduledThreadPool.schedule(new Thread(mWindow), 3, TimeUnit.SECONDS);
        scheduledThreadPool.schedule(new Thread(mWindow), 3, TimeUnit.SECONDS);
        scheduledThreadPool.schedule(new Thread(mWindow), 3, TimeUnit.SECONDS);
    }
}

//执行结果(3秒后输出)
pool-1-thread-1售出一张票,剩余票数: 4
pool-1-thread-2售出一张票,剩余票数: 3
pool-1-thread-2售出一张票,剩余票数: 2
pool-1-thread-2售出一张票,剩余票数: 1
pool-1-thread-2售出一张票,剩余票数: 0
车票已售罄!


4.newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。

public class ThreadTest {
    public static void main(String [] args) {
        ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); 
        TicketWidow mWindow = new TicketWidow(10);
        singleThreadExecutor.execute(new Thread(mWindow));
        singleThreadExecutor.execute(new Thread(mWindow));
        singleThreadExecutor.execute(new Thread(mWindow));
    }
}

//执行结果
pool-1-thread-1售出一张票,剩余票数: 4
pool-1-thread-1售出一张票,剩余票数: 3
pool-1-thread-1售出一张票,剩余票数: 2
pool-1-thread-1售出一张票,剩余票数: 1
pool-1-thread-1售出一张票,剩余票数: 0

 

标签: 线程池
共有 人打赏支持
粉丝 2
博文 33
码字总数 18901
×
kimyeongnam
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: