Java中怎么固定線程池大小

今天就跟大家聊聊有關(guān)Java中怎么固定線程池大小,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

目前成都創(chuàng)新互聯(lián)公司已為成百上千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁空間、網(wǎng)站托管、服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、電白網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

固定大小的Java線程池

Java線程池代碼

  1. import java.util.concurrent.Executors;   

  2. import java.util.concurrent.ExecutorService;   

  3. /**   

  4. * Java線程:線程池-   

  5. *   

  6. * @author Administrator 2009-11-4 23:30:44   

  7. */   

  8. public class Test {   

  9. public static void main(String[] args) {   

  10. //創(chuàng)建一個(gè)可重用固定線程數(shù)的線程池   

  11. ExecutorService pool = Executors.newFixedThreadPool(2);   

  12. //創(chuàng)建實(shí)現(xiàn)了Runnable接口對(duì)象,Thread對(duì)象當(dāng)然也實(shí)現(xiàn)了Runnable接口   

  13. Thread t1 = new MyThread();   

  14. Thread t2 = new MyThread();   

  15. Thread t3 = new MyThread();   

  16. Thread t4 = new MyThread();   

  17. Thread t5 = new MyThread();   

  18. //將線程放入池中進(jìn)行執(zhí)行   

  19. pool.execute(t1);   

  20. pool.execute(t2);   

  21. pool.execute(t3);   

  22. pool.execute(t4);   

  23. pool.execute(t5);   

  24. //關(guān)閉線程池   

  25. pool.shutdown();   

  26. }   

  27. }   

  28. class MyThread extends Thread{   

  29. @Override   

  30. public void run() {   

  31. System.out.println(Thread.currentThread().getName()+
    "正在執(zhí)行。。。");   

  32. }   

  33. }   

  34. import java.util.concurrent.Executors;   

  35. import java.util.concurrent.ExecutorService;   

  36. /**   

  37. * Java線程:線程池-   

  38. *   

  39. * @author Administrator 2009-11-4 23:30:44   

  40. */   

  41. public class Test {   

  42. public static void main(String[] args) {   

  43. //創(chuàng)建一個(gè)可重用固定線程數(shù)的線程池   

  44. ExecutorService pool = Executors.newFixedThreadPool(2);   

  45. //創(chuàng)建實(shí)現(xiàn)了Runnable接口對(duì)象,Thread對(duì)象當(dāng)然也實(shí)現(xiàn)了Runnable接口   

  46. Thread t1 = new MyThread();   

  47. Thread t2 = new MyThread();   

  48. Thread t3 = new MyThread();   

  49. Thread t4 = new MyThread();   

  50. Thread t5 = new MyThread();   

  51. //將線程放入池中進(jìn)行執(zhí)行   

  52. pool.execute(t1);   

  53. pool.execute(t2);   

  54. pool.execute(t3);   

  55. pool.execute(t4);   

  56. pool.execute(t5);   

  57. //關(guān)閉線程池   

  58. pool.shutdown();   

  59. }   

  60. }   

  61. class MyThread extends Thread{   

  62. @Override   

  63. public void run() {   

  64. System.out.println(Thread.currentThread().getName()+
    "正在執(zhí)行。。。");   

  65. }  

Java線程池代碼

pool-1-thread-1正在執(zhí)行。。。   pool-1-thread-1正在執(zhí)行。。。   pool-1-thread-1正在執(zhí)行。。。   pool-1-thread-1正在執(zhí)行。。。   pool-1-thread-2正在執(zhí)行。。。   Process finished with exit code 0

看完上述內(nèi)容,你們對(duì)Java中怎么固定線程池大小有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

分享文章:Java中怎么固定線程池大小
文章地址:http://muchs.cn/article44/jpiiee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)搜索引擎優(yōu)化、響應(yīng)式網(wǎng)站微信公眾號(hào)、ChatGPT品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化