Java单任务延迟连接池在四代码基础上,做改动,这个就需要我们不选的学习,下面我们就看看如何才能更好的使用。希望我们在下面的使用中大家更了解相关的代码。
Java <http://developer.51cto.com/art/200512/15883.htm>单任务延迟代码
创建一个单线程执行程序,它可安排在给定延迟后运行命令或者定期地执行。
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<Runnable> bqueue = new ArrayBlockingQueue
<Runnable>(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.//将线程放入池中进行执行
① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;
② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。