這篇文章主要講解了“Java線程池的使用實(shí)例”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Java線程池的使用實(shí)例”吧!
10年積累的網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有三山免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
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(); //獲取開始時(shí)間 ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);//設(shè)置線程池的最大線程數(shù) for (int i = 0; i < 10; i++) { final int index = i;//一般多線程并發(fā)都用final fixedThreadPool.execute(new Runnable() { public void run() { System.out.println(index + " " + Thread.currentThread().getName()); } }); } long endTime = System.currentTimeMillis(); //獲取結(jié)束時(shí)間 System.out.println("程序運(yùn)行時(shí)間:" + (endTime - startTime) + "ms"); } }
輸出結(jié)果: (當(dāng)然輸出結(jié)果不是固定的,不過線程數(shù)一定不會(huì)超過5個(gè))
可以看到 Thread.currentThread().getName() 拿到的name只有5種,說明最大線程數(shù)控制在 5 個(gè)
工作隊(duì)列用了LinkedBlockingQueue ,無界隊(duì)列,當(dāng)任務(wù)多而線程數(shù)少時(shí),任務(wù)會(huì)存在隊(duì)列里,容易內(nèi)存溢出。
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()); } }); } } }
輸出結(jié)果的 Thread.currentThread().getName() 拿到的name有一兩千種(當(dāng)然不同環(huán)境和配置的機(jī)器的結(jié)果最大線程數(shù)是不同的)
工作隊(duì)列使用SynchronousQueue同步隊(duì)列。會(huì)根據(jù)任務(wù)數(shù)創(chuàng)建線程,數(shù)量太大容易導(dǎo)致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()); } }); } } }
輸出結(jié)果:無論循環(huán)100次還是100000次,輸出結(jié)果Thread.currentThread().getName()的值都會(huì)是
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(); } } }); } } }
輸出結(jié)果:每隔10s就會(huì)輸出10行結(jié)果
**使用 ScheduledExecutorService 的 scheduleAtFixedRate方法可以設(shè)置延時(shí)和執(zhí)行間隔
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.
意思是創(chuàng)建并執(zhí)行一個(gè)在給定初始延遲后首次啟用的定期操作,后續(xù)操作具有給定的周期;也就是將在 initialDelay 后開始執(zhí)行,然后在 initialDelay+period 后執(zhí)行,接著在 initialDelay + 2 * period 后執(zhí)行,依此類推。
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); } }
從輸出結(jié)果可以看出,延時(shí)5s后每隔10s會(huì)輸出一次當(dāng)前時(shí)間。
**使用ScheduledExecutorService的schedule可以設(shè)置首次執(zhí)行延時(shí)
schedule(Runnable command, long delay, TimeUnit unit)
Creates and executes a one-shot action that becomes enabled after the given delay.
創(chuàng)建并執(zhí)行在給定延遲后啟用的一次性操作。
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); } } }
輸出結(jié)果:運(yùn)行3s后會(huì)輸出10行結(jié)果,而不會(huì)每隔3s輸出一行。
感謝各位的閱讀,以上就是“Java線程池的使用實(shí)例”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)Java線程池的使用實(shí)例這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!