Java單任務(wù)延遲相關(guān)代碼的學(xué)習(xí)筆記
作者:佚名
Java單任務(wù)延遲如何才能更好的使用呢?這就要求我們進行相關(guān)代碼的學(xué)習(xí)。下面就是有關(guān)于Java單任務(wù)延遲相關(guān)代碼的介紹。
Java單任務(wù)延遲連接池在四代碼基礎(chǔ)上,做改動,這個就需要我們不選的學(xué)習(xí),下面我們就看看如何才能更好的使用。希望我們在下面的使用中大家更了解相關(guān)的代碼。
Java單任務(wù)延遲代碼
創(chuàng)建一個單線程執(zhí)行程序,它可安排在給定延遲后運行命令或者定期地執(zhí)行。
- ScheduledExecutorService pool = Executors.newSingleThread
ScheduledExecutor();
創(chuàng)建一個單線程執(zhí)行程序,它可安排在給定延遲后運行命令或者定期地執(zhí)行。
- ScheduledExecutorService pool = Executors.newSingle
ThreadScheduledExecutor();
Java代碼
- pool-1-thread-1正在執(zhí)行。。。
- pool-1-thread-1正在執(zhí)行。。。
- pool-1-thread-1正在執(zhí)行。。。
- pool-1-thread-1正在執(zhí)行。。。
- pool-1-thread-1正在執(zhí)行。。。
- Process finished with exit code 0
- pool-1-thread-1正在執(zhí)行。。。
- pool-1-thread-1正在執(zhí)行。。。
- pool-1-thread-1正在執(zhí)行。。。
- pool-1-thread-1正在執(zhí)行。。。
- pool-1-thread-1正在執(zhí)行。。。
- Process finished with exit code 0
自定義線程池
Java代碼
- import java.util.concurrent.ArrayBlockingQueue;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.ThreadPoolExecutor;
- import java.util.concurrent.TimeUnit;
- /**
- * Java線程:線程池-自定義線程池
- *
- * @author Administrator 2009-11-4 23:30:44
- */
- public class Test {
- public static void main(String[] args) {
- //創(chuàng)建等待隊列
- BlockingQueue<Runnable> bqueue = new ArrayBlockingQueue
<Runnable>(20);- //創(chuàng)建一個單線程執(zhí)行程序,它可安排在給定延遲后運行命令或者定期地執(zhí)行。
- ThreadPoolExecutor pool = new ThreadPoolExecutor
(2,3,2,TimeUnit.MILLISECONDS,bqueue);- //創(chuàng)建實現(xiàn)了Runnable接口對象,Thread對象當(dāng)然也實現(xiàn)了Runnable接口
- Thread t1 = new MyThread();
- Thread t2 = new MyThread();
- Thread t3 = new MyThread();
- Thread t4 = new MyThread();
- Thread t5 = new MyThread();
- Thread t6 = new MyThread();
- Thread t7 = new MyThread();
- //將線程放入池中進行執(zhí)行
- pool.execute(t1);
- pool.execute(t2);
- pool.execute(t3);
- pool.execute(t4);
- pool.execute(t5);
- pool.execute(t6);
- pool.execute(t7);
- //關(guān)閉線程池
- pool.shutdown();
- }
- }
- class MyThread extends Thread {
- @Override
- public void run() {
- System.out.println(Thread.currentThread().getName() +
"正在執(zhí)行。。。");- try {
- Thread.sleep(100L);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- import java.util.concurrent.ArrayBlockingQueue;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.ThreadPoolExecutor;
- import java.util.concurrent.TimeUnit;
- /**
- * Java線程:線程池-自定義線程池
- *
- * @author Administrator 2009-11-4 23:30:44
- */
- public class Test {
- public static void main(String[] args) {
- //創(chuàng)建等待隊列
- BlockingQueue<Runnable> bqueue = new ArrayBlockingQueue
<Runnable>(20);- //創(chuàng)建一個單線程執(zhí)行程序,它可安排在給定延遲后運行命令或者定期地執(zhí)行。
- ThreadPoolExecutor pool = new ThreadPoolExecutor
(2,3,2,TimeUnit.MILLISECONDS,bqueue);- //創(chuàng)建實現(xiàn)了Runnable接口對象,Thread對象當(dāng)然也實現(xiàn)了Runnable接口
- Thread t1 = new MyThread();
- Thread t2 = new MyThread();
- Thread t3 = new MyThread();
- Thread t4 = new MyThread();
- Thread t5 = new MyThread();
- Thread t6 = new MyThread();
- Thread t7 = new MyThread();
- //將線程放入池中進行執(zhí)行
- pool.execute(t1);
- pool.execute(t2);
- pool.execute(t3);
- pool.execute(t4);
- pool.execute(t5);
- pool.execute(t6);
- pool.execute(t7);
- //關(guān)閉線程池
- pool.shutdown();
- }
- }
- class MyThread extends Thread {
- @Override
- public void run() {
- System.out.println(Thread.currentThread().getName() +
"正在執(zhí)行。。。");- try {
- Thread.sleep(100L);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
Java代碼
- pool-1-thread-1正在執(zhí)行。。。
- pool-1-thread-2正在執(zhí)行。。。
- pool-1-thread-2正在執(zhí)行。。。
- pool-1-thread-1正在執(zhí)行。。。
- pool-1-thread-2正在執(zhí)行。。。
- pool-1-thread-1正在執(zhí)行。。。
- pool-1-thread-2正在執(zhí)行。。。
- Process finished with exit code 0
- pool-1-thread-1正在執(zhí)行。。。
- pool-1-thread-2正在執(zhí)行。。。
- pool-1-thread-2正在執(zhí)行。。。
- pool-1-thread-1正在執(zhí)行。。。
- pool-1-thread-2正在執(zhí)行。。。
- pool-1-thread-1正在執(zhí)行。。。
- pool-1-thread-2正在執(zhí)行。。。
以上就是對Java單任務(wù)延遲的相關(guān)代碼介紹。
【編輯推薦】
責(zé)任編輯:張浩
來源:
博客園