java--執行緒池--建立執行緒池的幾種方式與執行緒池操作詳解

專注的阿熊發表於2021-07-13

package threadPool.java;

import org.junit.Test;

import java.util.concurrent.Executors;

import java.util.concurrent.ScheduledExecutorService;

import java.util.concurrent.TimeUnit;

public class ScheduledThreadPoolTest {

     @Test

     public void scheduleTest(){

         ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3);

         for (int i = 0; i < 10; i++) {

             scheduledExecutorService.schedule(new Task(i),2, TimeUnit.SECONDS);

         }

         System.out.println("exit");

     }

     @Test

     public void scheduleAtFixedRateTest(){

         ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1,new MyThreadFactory());

         scheduledExecutorService.scheduleAtFixedRate(() -> {

             String str = Thread.currentThread().getName();

             System.out.println(str+" 執行開始 ");

             System.out.println(" · ");

             try {

                 // 任務執行 1

                 Thread.sleep(1000);

             } catch (InterruptedException e) {

                 e.printStackTrace();

             }

             System.out.println(str+" 執行結束 ");

             System.out.println(" · ");

             System.out.println(" · ");

             // 初始延遲 2 秒,以後每次執行任務延遲 3 秒,上一次執行開始到下一次執行開始的時間為 3

         }, 2, 3, TimeUnit.SECONDS);

         System.out.println("exit");

     }

     @Test

     public void scheduleWithFixedDelayTest() {

         ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2,new MyThreadFactory());

         scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {

             @Override

             public void run() {

                 String str = Thread.currentThread().getName();

                 System.out.println(str+" 執行開始 ");

                 System.out.println(" · ");

                 try {

                     // 任務執行 1

                     Thread.sleep(1000);

                 } catch (InterruptedException e) {

                     e.printStackTrace();

                 }

                 System.out.println(str+" 執行結束 ");

                 System.out.println(" · ");

                 System.out.println(" · ");

                 System.out.println(" · ");

                 // 初始延遲 2 秒,外匯跟單gendan5.com以後每次執行任務延遲 3 秒,上一次執行結束到下一次執行開始的時間為 3

             }

         }, 2, 3, TimeUnit.SECONDS);

         System.out.println("exit");

     }

     @Test

     public void newSingleThreadScheduledExecutorTest() {

         ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new MyThreadFactory());

         scheduledExecutorService.scheduleWithFixedDelay(() -> {

             String str = Thread.currentThread().getName();

             System.out.println(str+" 執行開始 ");

             try {

                 // 任務執行 1

                 Thread.sleep(1000);

             } catch (InterruptedException e) {

                 e.printStackTrace();

             }

             System.out.println(" 執行結束 ");

             // 初始延遲 2 秒,以後每次執行任務延遲 3 秒,上一次執行結束到下一次執行開始的時間為 3

         }, 2, 3, TimeUnit.SECONDS);

         System.out.println("exit");

     }

     public static void main(String[] args) {

         ScheduledThreadPoolTest scheduledThreadPoolTest = new ScheduledThreadPoolTest();

         // scheduledThreadPoolTest.scheduleWithFixedDelayTest();

         // scheduledThreadPoolTest.scheduleAtFixedRateTest();

          scheduledThreadPoolTest.newSingleThreadScheduledExecutorTest();

     }

}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2781133/,如需轉載,請註明出處,否則將追究法律責任。

相關文章