您好,登錄后才能下訂單哦!
這篇文章主要講解了“Java線程池的使用實例”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Java線程池的使用實例”吧!
TestThreadPool.java
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class TestThreadPool { public static void main(String[] args) { long startTime = System.currentTimeMillis(); //獲取開始時間 ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);//設置線程池的最大線程數 for (int i = 0; i < 10; i++) { final int index = i;//一般多線程并發都用final fixedThreadPool.execute(new Runnable() { public void run() { System.out.println(index + " " + Thread.currentThread().getName()); } }); } long endTime = System.currentTimeMillis(); //獲取結束時間 System.out.println("程序運行時間:" + (endTime - startTime) + "ms"); } }
輸出結果: (當然輸出結果不是固定的,不過線程數一定不會超過5個)
可以看到 Thread.currentThread().getName() 拿到的name只有5種,說明最大線程數控制在 5 個
工作隊列用了LinkedBlockingQueue ,無界隊列,當任務多而線程數少時,任務會存在隊列里,容易內存溢出。
TestThreadPool1.java
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class TestThreadPool1 { public static void main(String[] args) { ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); for (int i = 0; i < 10000; i++) { final int index = i; cachedThreadPool.execute(new Runnable() { public void run() { System.out.println(index + " " + Thread.currentThread().getName()); } }); } } }
輸出結果的 Thread.currentThread().getName() 拿到的name有一兩千種(當然不同環境和配置的機器的結果最大線程數是不同的)
工作隊列使用SynchronousQueue同步隊列。會根據任務數創建線程,數量太大容易導致cpu使用率100% 99%
TestThreadPool2.java
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class TestThreadPool2 { public static void main(String[] args) { ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); for (int i = 0; i < 10000; i++) { final int index = i; singleThreadExecutor.execute(new Runnable() { public void run() { System.out.println(index + " " + Thread.currentThread().getName()); } }); } } }
輸出結果:無論循環100次還是100000次,輸出結果Thread.currentThread().getName()的值都會是
pool-1-thread-1
TestThreadPool3.java
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class TestThreadPool3 { public static void main(String[] args) { ExecutorService singleThreadExecutor = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) { final int index = i; singleThreadExecutor.execute(new Runnable() { public void run() { try { while(true) { System.out.println(index + " " + Thread.currentThread().getName()); Thread.sleep(10 * 1000); } } catch (InterruptedException e) { e.printStackTrace(); } } }); } } }
輸出結果:每隔10s就會輸出10行結果
**使用 ScheduledExecutorService 的 scheduleAtFixedRate方法可以設置延時和執行間隔
scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on.
意思是創建并執行一個在給定初始延遲后首次啟用的定期操作,后續操作具有給定的周期;也就是將在 initialDelay 后開始執行,然后在 initialDelay+period 后執行,接著在 initialDelay + 2 * period 后執行,依此類推。
TestThreadPool4.java
import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class TestThreadPool4 { public static void main(String[] args) { ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5); System.out.println(System.currentTimeMillis()); scheduledThreadPool.scheduleAtFixedRate(new Runnable() { public void run() { System.out.println(System.currentTimeMillis()); } }, 5, 10, TimeUnit.SECONDS); } }
從輸出結果可以看出,延時5s后每隔10s會輸出一次當前時間。
**使用ScheduledExecutorService的schedule可以設置首次執行延時
schedule(Runnable command, long delay, TimeUnit unit)
Creates and executes a one-shot action that becomes enabled after the given delay.
創建并執行在給定延遲后啟用的一次性操作。
TestThreadPool5.java
import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class TestThreadPool5 { public static void main(String[] args) { ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5); for (int i = 0; i < 10; i++) { final int index = i; scheduledThreadPool.schedule(new Runnable() { public void run() { System.out.println(index + " " + Thread.currentThread().getName()); } }, 3, TimeUnit.SECONDS); } } }
輸出結果:運行3s后會輸出10行結果,而不會每隔3s輸出一行。
感謝各位的閱讀,以上就是“Java線程池的使用實例”的內容了,經過本文的學習后,相信大家對Java線程池的使用實例這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。