Java单任务延迟连接池在四代码基础上做改动这个就需要我们不选的学习下面我们就看看如何才能更好的使用希望我们在下面的使用中大家更了解相关的代码
Java <>单任务延迟代码
创建一个单线程执行程序它可安排在给定延迟后运行命令或者定期地执行
ScheduledExecutorService pool = ExecutorsnewSingleThread
ScheduledExecutor();
创建一个单线程执行程序它可安排在给定延迟后运行命令或者定期地执行
ScheduledExecutorService pool = ExecutorsnewSingle
ThreadScheduledExecutor();
Java代码
poolthread正在执行
poolthread正在执行
poolthread正在执行
poolthread正在执行
poolthread正在执行
Process finished with exit code
poolthread正在执行
poolthread正在执行
poolthread正在执行
poolthread正在执行
poolthread正在执行
Process finished with exit code
自定义线程池
Java代码
import ncurrentArrayBlockingQueue;
import ncurrentBlockingQueue;
import ncurrentThreadPoolExecutor;
import ncurrentTimeUnit;
/**
* Java线程线程池自定义线程池
*
* @author Administrator ::
*/
public class Test {
public static void main(String[] args) {
//创建等待队列
BlockingQueue<Runnable> bqueue = new ArrayBlockingQueue
<Runnable>();
//创建一个单线程执行程序它可安排在给定延迟后运行命令或者定期地执行
ThreadPoolExecutor pool = new ThreadPoolExecutor
(TimeUnitMILLISECONDSbqueue);
//创建实现了Runnable接口对象Thread对象当然也实现了Runnable接口
Thread t = new MyThread();
Thread t = new MyThread();
Thread t = new MyThread();
Thread t = new MyThread();
Thread t = new MyThread();
Thread t = new MyThread();
Thread t = new MyThread();
//将线程放入池中进行执行
poolexecute(t);
poolexecute(t);
poolexecute(t);
poolexecute(t);
poolexecute(t);
poolexecute(t);
poolexecute(t);
//关闭线程池
poolshutdown();
}
}
class MyThread extends Thread {
@Override
public void run() {
Systemoutprintln(ThreadcurrentThread()getName() +
正在执行);
try {
Threadsleep(L);
} catch (InterruptedException e) {
eprintStackTrace();
}
}
}
import ncurrentArrayBlockingQueue;
import ncurrentBlockingQueue;
import ncurrentThreadPoolExecutor;
import ncurrentTimeUnit;
/**
* Java线程线程池自定义线程池
*
* @author Administrator ::
*/
public class Test {
public static void main(String[] args) {
//创建等待队列
BlockingQueue<Runnable> bqueue = new ArrayBlockingQueue
<Runnable>();
//创建一个单线程执行程序它可安排在给定延迟后运行命令或者定期地执行
ThreadPoolExecutor pool = new ThreadPoolExecutor
(TimeUnitMILLISECONDSbqueue);
//创建实现了Runnable接口对象Thread对象当然也实现了Runnable接口
Thread t = new MyThread();
Thread t = new MyThread();
Thread t = new MyThread();
Thread t = new MyThread();
Thread t = new MyThread();
Thread t = new MyThread();
Thread t = new MyThread();
//将线程放入池中进行执行
poolexecute(t);
poolexecute(t);
poolexecute(t);
poolexecute(t);
poolexecute(t);
poolexecute(t);
poolexecute(t);
//关闭线程池
poolshutdown();
}
}
class MyThread extends Thread {
@Override
public void run() {
Systemoutprintln(ThreadcurrentThread()getName() +
正在执行);
try {
Threadsleep(L);
} catch (InterruptedException e) {
eprintStackTrace();
}
}
}
Java代码
poolthread正在执行
poolthread正在执行
poolthread正在执行
poolthread正在执行
poolthread正在执行
poolthread正在执行
poolthread正在执行
Process finished with exit code
poolthread正在执行
poolthread正在执行
poolthread正在执行
poolthread正在执行
poolthread正在执行
poolthread正在执行
poolthread正在执行
以上就是对Java单任务延迟的相关代码介绍