Java单任务延迟相关代码的学习笔记

开发 后端
Java单任务延迟如何才能更好的使用呢?这就要求我们进行相关代码的学习。下面就是有关于Java单任务延迟相关代码的介绍。

Java单任务延迟连接池在四代码基础上,做改动,这个就需要我们不选的学习,下面我们就看看如何才能更好的使用。希望我们在下面的使用中大家更了解相关的代码。

Java单任务延迟代码

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

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

 

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

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

 

Java代码

  1. pool-1-thread-1正在执行。。。   
  2. pool-1-thread-1正在执行。。。   
  3. pool-1-thread-1正在执行。。。   
  4. pool-1-thread-1正在执行。。。   
  5. pool-1-thread-1正在执行。。。   
  6. Process finished with exit code 0   
  7. pool-1-thread-1正在执行。。。   
  8. pool-1-thread-1正在执行。。。   
  9. pool-1-thread-1正在执行。。。   
  10. pool-1-thread-1正在执行。。。   
  11. pool-1-thread-1正在执行。。。   
  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. //创建等待队列   
  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代码

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

以上就是对Java单任务延迟的相关代码介绍。

【编辑推荐】

  1. Java线程控制权源代码的深入探讨
  2. Java线程同步引用基本代码介绍
  3. Java线程模型如何完善相关的数据处理
  4. Java线程同步锁解决共享数据安全
  5. Java线程检测基本的问题猜想
责任编辑:张浩 来源: 博客园
相关推荐

2022-05-31 09:36:18

JDKDelayQueueRedis

2011-08-05 14:03:39

Objective-C 对象 模板

2021-12-13 05:54:30

Windows 11操作系统微软

2010-03-17 19:24:38

Java多线程循环

2010-03-19 16:51:53

Java Socket

2009-12-28 11:08:34

ADO 实例

2013-04-03 14:58:43

Android学习笔记实用代码合集

2024-01-31 08:01:36

Go延迟队列语言

2024-04-09 10:40:04

2010-03-15 17:05:39

Java任务队列

2009-06-22 14:28:00

java接口

2009-06-29 09:00:14

JSFJava

2009-11-16 13:18:10

PHP上传图片代码

2009-06-17 17:09:02

Java异常Java断言

2010-03-22 13:25:47

Java Socket

2022-06-27 23:49:21

数据仓库资源不足集群

2010-03-18 16:19:02

Java自定义线程池

2009-06-17 14:21:39

core java

2021-02-14 14:31:35

机器学习Python模型

2010-07-30 13:08:38

Flex调用JavaS
点赞
收藏

51CTO技术栈公众号