Java单任务延迟连接池在四代码基础上,做改动,这个就需要我们不选的学习,下面我们就看看如何才能更好的使用。希望我们在下面的使用中大家更了解相关的代码。
Java
单任务延迟代码
创建一个单线程执行程序,它可安排在给定延迟后运行命令或者定期地执行。
1.ScheduledExecutorService pool = Executors.newSingleThread
ScheduledExecutor();
创建一个单线程执行程序,它可安排在给定延迟后运行命令或者定期地执行。
2.ScheduledExecutorService pool = Executors.newSingle
ThreadScheduledExecutor();
Java代码
3.pool-1-thread-1正在执行。。。
4.pool-1-thread-1正在执行。。。
5.pool-1-thread-1正在执行。。。
6.pool-1-thread-1正在执行。。。
7.pool-1-thread-1正在执行。。。
8.Process finished with exit code 0
9.pool-1-thread-1正在执行。。。
10.pool-1-thread-1正在执行。。。
11.pool-1-thread-1正在执行。。。
12.pool-1-thread-1正在执行。。。
13.pool-1-thread-1正在执行。。。
14.Process finished with exit code 0
自定义线程池
Java代码
15.import java.util.concurrent.ArrayBlockingQueue;
16.import java.util.concurrent.BlockingQueue;
17.import java.util.concurrent.ThreadPoolExecutor;
18.import java.util.concurrent.TimeUnit;
19./**
20.* Java线程:线程池-自定义线程池
21.*
22.* @author Administrator 2009-11-4 23:30:44
23.*/
24.public class Test {
25.public static void main(String[] args) {
26.//创建等待队列
27.BlockingQueue
bqueue = new ArrayBlockingQueue
(20);
28.//创建一个单线程执行程序,它可安排在给定延迟后运行命令或者定期地执行。
29.ThreadPoolExecutor pool = new ThreadPoolExecutor
(2,3,2,TimeUnit.MILLISECONDS,bqueue);
30.//创建实现了Runnable接口对象,Thread对象当然也实现了Runnable接口
31.Thread t1 = new MyThread();
32.Thread t2 = new MyThread();
33.Thread t3 = new MyThread();
34.Thread t4 = new MyThread();
35.Thread t5 = new MyThread();
36.Thread t6 = new MyThread();
37.Thread t7 = new MyThread();
38.//将线程放入池中进行执行