自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java單任務(wù)延遲相關(guān)代碼的學(xué)習(xí)筆記

開發(fā) 后端
Java單任務(wù)延遲如何才能更好的使用呢?這就要求我們進行相關(guān)代碼的學(xué)習(xí)。下面就是有關(guān)于Java單任務(wù)延遲相關(guān)代碼的介紹。

Java單任務(wù)延遲連接池在四代碼基礎(chǔ)上,做改動,這個就需要我們不選的學(xué)習(xí),下面我們就看看如何才能更好的使用。希望我們在下面的使用中大家更了解相關(guān)的代碼。

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

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

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

 

創(chuàng)建一個單線程執(zhí)行程序,它可安排在給定延遲后運行命令或者定期地執(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)建等待隊列   
  13. BlockingQueue<Runnable> bqueue = new ArrayBlockingQueue
    <Runnable>(20);   
  14. //創(chuàng)建一個單線程執(zhí)行程序,它可安排在給定延遲后運行命令或者定期地執(zhí)行。   
  15. ThreadPoolExecutor pool = new ThreadPoolExecutor
    (2,3,2,TimeUnit.MILLISECONDS,bqueue);   
  16. //創(chuàng)建實現(xiàn)了Runnable接口對象,Thread對象當(dāng)然也實現(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. //將線程放入池中進行執(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)建等待隊列   
  60. BlockingQueue<Runnable> bqueue = new ArrayBlockingQueue
    <Runnable>(20);   
  61. //創(chuàng)建一個單線程執(zhí)行程序,它可安排在給定延遲后運行命令或者定期地執(zhí)行。   
  62. ThreadPoolExecutor pool = new ThreadPoolExecutor
    (2,3,2,TimeUnit.MILLISECONDS,bqueue);   
  63. //創(chuàng)建實現(xiàn)了Runnable接口對象,Thread對象當(dāng)然也實現(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. //將線程放入池中進行執(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í)行。。。  

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

【編輯推薦】

  1. Java線程控制權(quán)源代碼的深入探討
  2. Java線程同步引用基本代碼介紹
  3. Java線程模型如何完善相關(guān)的數(shù)據(jù)處理
  4. Java線程同步鎖解決共享數(shù)據(jù)安全
  5. Java線程檢測基本的問題猜想
責(zé)任編輯:張浩 來源: 博客園
相關(guān)推薦

2022-05-31 09:36:18

JDKDelayQueueRedis

2024-12-31 00:00:00

RabbitMQ插件代碼

2011-08-05 14:03:39

Objective-C 對象 模板

2024-12-17 15:39:33

2013-04-03 14:58:43

Android學(xué)習(xí)筆記實用代碼合集

2021-12-13 05:54:30

Windows 11操作系統(tǒng)微軟

2009-12-28 11:08:34

ADO 實例

2010-03-19 16:51:53

Java Socket

2009-06-29 09:00:14

JSFJava

2009-06-22 14:28:00

java接口

2010-03-17 19:24:38

Java多線程循環(huán)

2009-11-16 13:18:10

PHP上傳圖片代碼

2024-01-31 08:01:36

Go延遲隊列語言

2024-10-22 16:39:07

2009-06-17 17:09:02

Java異常Java斷言

2024-04-09 10:40:04

2010-03-15 17:05:39

Java任務(wù)隊列

2009-06-17 14:21:39

core java

2010-07-30 13:08:38

Flex調(diào)用JavaS

2009-06-29 16:50:27

Java集合框架
點贊
收藏

51CTO技術(shù)棧公眾號