重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Java中怎么实现单任务延迟

今天就跟大家聊聊有关Java中怎么实现单任务延迟,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

“只有客户发展了,才有我们的生存与发展!”这是创新互联的服务宗旨!把网站当作互联网产品,产品思维更注重全局思维、需求分析和迭代思维,在网站建设中就是为了建设一个不仅审美在线,而且实用性极高的网站。创新互联对网站设计、成都网站设计、网站制作、网站开发、网页设计、网站优化、网络推广、探索永无止境。

Java单任务延迟代码

创建一个单线程执行程序,它可安排在给定延迟后运行命令或者定期地执行。

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

创建一个单线程执行程序,它可安排在给定延迟后运行命令或者定期地执行。

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

Java代码

pool-1-thread-1正在执行。。。   pool-1-thread-1正在执行。。。   pool-1-thread-1正在执行。。。   pool-1-thread-1正在执行。。。   pool-1-thread-1正在执行。。。   Process finished with exit code 0   pool-1-thread-1正在执行。。。   pool-1-thread-1正在执行。。。   pool-1-thread-1正在执行。。。   pool-1-thread-1正在执行。。。   pool-1-thread-1正在执行。。。   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. //创建等待队列   

  13. BlockingQueue bqueue = new ArrayBlockingQueue
    (20);   

  14. //创建一个单线程执行程序,它可安排在给定延迟后运行命令或者定期地执行。   

  15. ThreadPoolExecutor pool = new ThreadPoolExecutor
    (2,3,2,TimeUnit.MILLISECONDS,bqueue);   

  16. //创建实现了Runnable接口对象,Thread对象当然也实现了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. //将线程放入池中进行执行   

  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. //关闭线程池   

  33. pool.shutdown();   

  34. }   

  35. }   

  36. class MyThread extends Thread {   

  37. @Override   

  38. public void run() {   

  39. System.out.println(Thread.currentThread().getName() + 
    "正在执行。。。");   

  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. //创建等待队列   

  60. BlockingQueue bqueue = new ArrayBlockingQueue
    (20);   

  61. //创建一个单线程执行程序,它可安排在给定延迟后运行命令或者定期地执行。   

  62. ThreadPoolExecutor pool = new ThreadPoolExecutor
    (2,3,2,TimeUnit.MILLISECONDS,bqueue);   

  63. //创建实现了Runnable接口对象,Thread对象当然也实现了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. //将线程放入池中进行执行   

  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. //关闭线程池   

  80. pool.shutdown();   

  81. }   

  82. }   

  83. class MyThread extends Thread {   

  84. @Override   

  85. public void run() {   

  86. System.out.println(Thread.currentThread().getName() + 
    "正在执行。。。");   

  87. try {   

  88. Thread.sleep(100L);   

  89. } catch (InterruptedException e) {   

  90. e.printStackTrace();   

  91. }   

  92. }   

  93. }  

Java代码

pool-1-thread-1正在执行。。。   pool-1-thread-2正在执行。。。   pool-1-thread-2正在执行。。。   pool-1-thread-1正在执行。。。   pool-1-thread-2正在执行。。。   pool-1-thread-1正在执行。。。   pool-1-thread-2正在执行。。。   Process finished with exit code 0   pool-1-thread-1正在执行。。。   pool-1-thread-2正在执行。。。   pool-1-thread-2正在执行。。。   pool-1-thread-1正在执行。。。   pool-1-thread-2正在执行。。。   pool-1-thread-1正在执行。。。   pool-1-thread-2正在执行。。。

看完上述内容,你们对Java中怎么实现单任务延迟有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


当前文章:Java中怎么实现单任务延迟
本文URL:http://cqcxhl.cn/article/igpcps.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP