Java中怎么实现单任务延迟

发布时间:2021-07-19 18:04:59 作者:Leah
来源:亿速云 阅读:359

今天就跟大家聊聊有关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<Runnable> bqueue = new ArrayBlockingQueue
    <Runnable>(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<Runnable> bqueue = new ArrayBlockingQueue
    <Runnable>(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中怎么实现单任务延迟有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. 简单任务调度系统
  2. SSIS中如何实现延迟验证

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:java 中怎么配置Socket多线程客户端

下一篇:python中PaddleOCR库的用法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》