日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線(xiàn)溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Java線(xiàn)程池如何固定自身的大小

Java線(xiàn)程池在我們學(xué)習(xí)的時(shí)候有很多的問(wèn)題,其實(shí)在很多的問(wèn)題我們都能在源代碼中找到我們想要的答案。希望大家有所收獲,首先我們來(lái)看看Java線(xiàn)程池大小的問(wèn)題。

創(chuàng)新互聯(lián)建站從2013年成立,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元伽師做網(wǎng)站,已為上家服務(wù),為伽師各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話(huà):028-86922220

在使用線(xiàn)程池之前,必須知道如何去創(chuàng)建一個(gè)線(xiàn)程池,在Java5中,需要了解的是java.util.concurrent.Executors類(lèi)的API,這個(gè)類(lèi)提供大量創(chuàng)建連接池的靜態(tài)方法,是必須掌握的。

固定大小的Java線(xiàn)程池

Java線(xiàn)程池代碼

 
 
 
  1. import java.util.concurrent.Executors;   
  2. import java.util.concurrent.ExecutorService;   
  3. /**   
  4. * Java線(xiàn)程:線(xiàn)程池-   
  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è)可重用固定線(xiàn)程數(shù)的線(xiàn)程池   
  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. //將線(xiàn)程放入池中進(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)閉線(xià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線(xiàn)程:線(xiàn)程池-   
  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è)可重用固定線(xiàn)程數(shù)的線(xiàn)程池   
  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. //將線(xiàn)程放入池中進(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)閉線(xià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線(xiàn)程池代碼

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

以上就是對(duì)Java線(xiàn)程池的詳細(xì)介紹。


分享名稱(chēng):Java線(xiàn)程池如何固定自身的大小
標(biāo)題來(lái)源:http://www.dlmjj.cn/article/dpdepis.html