java线程池笔记及相关代码
生活随笔
收集整理的這篇文章主要介紹了
java线程池笔记及相关代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
java線程池筆記:
線程池好處:
重用存在的線程,減少對象創建、消亡的開銷,性能佳。
可有效控制最大并發線程數,提高系統資源的使用率,同時避免過多資源競爭,避免堵塞。
提供定時執行、定期執行、單線程、并發數控制等功能。
java提供四個常用線程池
1.Executors是線程池頂級類。
2.ExecutorService 線程池類型.execute();
可緩存:newCachedThreadPool();
并發數控制:newFixedThreadPool(int num);
單線程:newSingelThreadPool();
定時執行、定期執行:newScheduledThreadPool();其二方法scheduleAtFixedRate(new Thread(), init延遲, 周期, TimeUnit.SECONDS);/scheduledthread.schedule(thread,time,unit);
代碼實現:
/*** java線程池* @author joker*/ package com.Threadp;import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit;class Threadtest implements Runnable {public int index;public Threadtest(int ind) {this.index = ind;}@Overridepublic void run() {// TODO 自動生成的方法存根System.out.print("\n第" + index + "線程被喚醒。。");int i = 0;while (i < 4) {try {Thread.sleep(500);System.out.print(index + "->");} catch (InterruptedException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}i++;}} }public class Threadpool {public static void main(String[] args) {// TODO 自動生成的方法存根// Executors線程池父類java.util.concurrent.Executor// 線程池好處:回收線程創立與消耗造成的不必要消耗// 高并發處理/*** newCachedThreadPool創建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。* newFixedThreadPool 創建一個定長線程池,可控制線程最大并發數,超出的線程會在隊列中等待。 newScheduledThreadPool* 創建一個定長線程池,支持定時及周期性任務執行。 newSingleThreadExecutor* 創建一個單線程化的線程池,它只會用唯一的工作線程來執行任務,保證所有任務按照指定順序(FIFO, LIFO, 優先級)執行。* *//** 可緩存的線程池 線程池為無限大,當執行第二個任務時第一個任務已經完成,會復用執行第一個任務的線程,而不用每次新建線程。*/System.out.println("\n===========================================");ExecutorService cachedthread = Executors.newCachedThreadPool();System.out.println("可緩存線程池創建。。。" + "&(等待執行性)");for (int i = 0; i < 3; i++) {try {Thread.sleep(i * 1000);System.out.println(i + "新線程被創建");} catch (InterruptedException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}cachedthread.execute(new Runnable() {@Overridepublic void run() {// TODO 自動生成的方法存根System.out.println("新線程被喚醒");}});}cachedthread.shutdown();System.out.println("\n可緩存線程池銷毀。。。");/** 定長線程池 可控制線程最大并發數,超出的線程會在隊列中等待*/System.out.println("\n===========================================");ExecutorService fixedthread = Executors.newFixedThreadPool(3);System.out.println("\n\n定長線程池創建。。。(長度3)" + "&(超出線程池則線程等待)");for (int i = 1; i < 6; i++)fixedthread.execute(new Threadtest(i));try {Thread.sleep(10000);} catch (InterruptedException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}fixedthread.shutdown();System.out.println("\n===========================================");System.out.println("\n定長線程池銷毀。。。等待執行型");System.out.println("\n\n定長線程池創建。。。" + "&(周期性執行,延遲執行)");ScheduledExecutorService scheduledthread = Executors.newScheduledThreadPool(4);// 周期執行System.out.println("周期執行2秒");// scheduleAtFixedRate(new Thread(), init延遲, 周期, TimeUnit.SECONDS);scheduledthread.scheduleAtFixedRate(new Threadtest(new Random().nextInt(5)), 0, 2, TimeUnit.SECONDS);System.out.println("延遲執行");scheduledthread.schedule(new Runnable() {@Overridepublic void run() {// TODO 自動生成的方法存根System.out.println("延遲執行5秒\n");}}, 5, TimeUnit.SECONDS);try {Thread.sleep(7000);} catch (InterruptedException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}scheduledthread.shutdown();System.out.println("\n定長線程池銷毀。。。周延型");System.out.println("\n===========================================");System.out.println("\n\n定長線程池創建。。。" + "&(單線程池)");ExecutorService singelthread=Executors.newSingleThreadExecutor();singelthread.execute(new Threadtest(9999));try {Thread.sleep(8000);} catch (InterruptedException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}singelthread.shutdown();System.out.println("\n定長線程池銷毀。。。單線程池型");}}總結
以上是生活随笔為你收集整理的java线程池笔记及相关代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于我在学习Javaweb时对Linux
- 下一篇: struts2关键配置及函数总结,