日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Java單任務(wù)延遲相關(guān)代碼的學(xué)習(xí)筆記

Java單任務(wù)延遲連接池在四代碼基礎(chǔ)上,做改動(dòng),這個(gè)就需要我們不選的學(xué)習(xí),下面我們就看看如何才能更好的使用。希望我們?cè)谙旅娴氖褂弥写蠹腋私庀嚓P(guān)的代碼。

Java單任務(wù)延遲代碼

創(chuàng)建一個(gè)單線程執(zhí)行程序,它可安排在給定延遲后運(yùn)行命令或者定期地執(zhí)行。

 
 
 
  1. ScheduledExecutorService pool = Executors.newSingleThread
    ScheduledExecutor(); 

 

創(chuàng)建一個(gè)單線程執(zhí)行程序,它可安排在給定延遲后運(yùn)行命令或者定期地執(zhí)行。

 
 
 
  1. ScheduledExecutorService pool = Executors.newSingle
    ThreadScheduledExecutor();  

 

Java代碼

 
 
 
  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-1正在執(zhí)行。。。   
  6. Process finished with exit code 0   
  7. pool-1-thread-1正在執(zhí)行。。。   
  8. pool-1-thread-1正在執(zhí)行。。。   
  9. pool-1-thread-1正在執(zhí)行。。。   
  10. pool-1-thread-1正在執(zhí)行。。。   
  11. pool-1-thread-1正在執(zhí)行。。。   
  12. Process finished with exit code 0  

 

自定義線程池

Java代碼

 
 
 
  1. import java.util.concurrent.ArrayBlockingQueue;   
  2. import java.util.concurrent.BlockingQueue;   
  3. import java.util.concurrent.ThreadPoolExecutor;   
  4. import java.util.concurrent.TimeUnit;   
  5. /**   
  6. * Java線程:線程池-自定義線程池   
  7. *   
  8. * @author Administrator 2009-11-4 23:30:44   
  9. */   
  10. public class Test {   
  11. public static void main(String[] args) {   
  12. //創(chuàng)建等待隊(duì)列   
  13. BlockingQueue bqueue = new ArrayBlockingQueue
    (20);   
  14. //創(chuàng)建一個(gè)單線程執(zhí)行程序,它可安排在給定延遲后運(yùn)行命令或者定期地執(zhí)行。   
  15. ThreadPoolExecutor pool = new ThreadPoolExecutor
    (2,3,2,TimeUnit.MILLISECONDS,bqueue);   
  16. //創(chuàng)建實(shí)現(xiàn)了Runnable接口對(duì)象,Thread對(duì)象當(dāng)然也實(shí)現(xiàn)了Runnable接口   
  17. Thread t1 = new MyThread();   
  18. Thread t2 = new MyThread();   
  19. Thread t3 = new MyThread();   
  20. Thread t4 = new MyThread();   
  21. Thread t5 = new MyThread();   
  22. Thread t6 = new MyThread();   
  23. Thread t7 = new MyThread();   
  24. //將線程放入池中進(jìn)行執(zhí)行   
  25. pool.execute(t1);   
  26. pool.execute(t2);   
  27. pool.execute(t3);   
  28. pool.execute(t4);   
  29. pool.execute(t5);   
  30. pool.execute(t6);   
  31. pool.execute(t7);   
  32. //關(guān)閉線程池   
  33. pool.shutdown();   
  34. }   
  35. }   
  36. class MyThread extends Thread {   
  37. @Override   
  38. public void run() {   
  39. System.out.println(Thread.currentThread().getName() + 
    "正在執(zhí)行。。。");   
  40. try {   
  41. Thread.sleep(100L);   
  42. } catch (InterruptedException e) {   
  43. e.printStackTrace();   
  44. }   
  45. }   
  46. }   
  47. import java.util.concurrent.ArrayBlockingQueue;   
  48. import java.util.concurrent.BlockingQueue;   
  49. import java.util.concurrent.ThreadPoolExecutor;   
  50. import java.util.concurrent.TimeUnit;   
  51.  
  52. /**   
  53. * Java線程:線程池-自定義線程池   
  54. *   
  55. * @author Administrator 2009-11-4 23:30:44   
  56. */   
  57. public class Test {   
  58. public static void main(String[] args) {   
  59. //創(chuàng)建等待隊(duì)列   
  60. BlockingQueue bqueue = new ArrayBlockingQueue
    (20);   
  61. //創(chuàng)建一個(gè)單線程執(zhí)行程序,它可安排在給定延遲后運(yùn)行命令或者定期地執(zhí)行。   
  62. ThreadPoolExecutor pool = new ThreadPoolExecutor
    (2,3,2,TimeUnit.MILLISECONDS,bqueue);   
  63. //創(chuàng)建實(shí)現(xiàn)了Runnable接口對(duì)象,Thread對(duì)象當(dāng)然也實(shí)現(xiàn)了Runnable接口   
  64. Thread t1 = new MyThread();   
  65. Thread t2 = new MyThread();   
  66. Thread t3 = new MyThread();   
  67. Thread t4 = new MyThread();   
  68. Thread t5 = new MyThread();   
  69. Thread t6 = new MyThread();   
  70. Thread t7 = new MyThread();   
  71. //將線程放入池中進(jìn)行執(zhí)行   
  72. pool.execute(t1);   
  73. pool.execute(t2);   
  74. pool.execute(t3);   
  75. pool.execute(t4);   
  76. pool.execute(t5);   
  77. pool.execute(t6);   
  78. pool.execute(t7);   
  79. //關(guān)閉線程池   
  80. pool.shutdown();   
  81. }   
  82. }   
  83. class MyThread extends Thread {   
  84. @Override   
  85. public void run() {   
  86. System.out.println(Thread.currentThread().getName() + 
    "正在執(zhí)行。。。");   
  87. try {   
  88. Thread.sleep(100L);   
  89. } catch (InterruptedException e) {   
  90. e.printStackTrace();   
  91. }   
  92. }   
  93. }  

 

Java代碼

 
 
 
  1. pool-1-thread-1正在執(zhí)行。。。   
  2. pool-1-thread-2正在執(zhí)行。。。   
  3. pool-1-thread-2正在執(zhí)行。。。   
  4. pool-1-thread-1正在執(zhí)行。。。   
  5. pool-1-thread-2正在執(zhí)行。。。   
  6. pool-1-thread-1正在執(zhí)行。。。   
  7. pool-1-thread-2正在執(zhí)行。。。   
  8. Process finished with exit code 0   
  9. pool-1-thread-1正在執(zhí)行。。。   
  10. pool-1-thread-2正在執(zhí)行。。。   
  11. pool-1-thread-2正在執(zhí)行。。。   
  12. pool-1-thread-1正在執(zhí)行。。。   
  13. pool-1-thread-2正在執(zhí)行。。。   
  14. pool-1-thread-1正在執(zhí)行。。。   
  15. pool-1-thread-2正在執(zhí)行。。。  

以上就是對(duì)Java單任務(wù)延遲的相關(guān)代碼介紹。

【編輯推薦】

  1. Java線程控制權(quán)源代碼的深入探討
  2. Java線程同步引用基本代碼介紹
  3. Java線程模型如何完善相關(guān)的數(shù)據(jù)處理
  4. Java線程同步鎖解決共享數(shù)據(jù)安全
  5. Java線程檢測(cè)基本的問(wèn)題猜想

分享名稱:Java單任務(wù)延遲相關(guān)代碼的學(xué)習(xí)筆記
網(wǎng)址分享:http://m.5511xx.com/article/cdsideh.html