Javaのユーザスレッドとスレッドガード

主な内容

  • ユーザーがすべてのスレッドを終了すると、プログラムはその後もまだデーモンスレッドが実行されて、終了します。
  • java.util.concurrentのスケジュールされたタスクのタイミングを含むすべてのユーザー、デフォルトで作成されたスレッドプールのスレッドを以下に示します。いくつかの統計クラス、クラスの監視スレッドタイマ動作がある場合、実際のプログラミングでは、スレッドは、好ましくは、デーモンスレッドとして提供されています。
  • 実行のスレッドプールのスレッドガードタイミングを作成する方法

すべてのユーザのスレッドが終了したら、デーモンスレッドがなくなっさえなくて、それが終了します

public class Test {
    public static void main(String[] args) {
        //创建一个用户线程
        Thread userThread=new Thread(){
            public void run(){
                for(int i=1;i<=5;i++){
                    try {
                        Thread.sleep(200);
                        System.out.println("用户线程第"+i+"次运行.....");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("用户线程退出.....");
            }
        };
        //创建一个用户模拟守护线程的线程
        Thread daemonThread=new Thread(){
            public void run(){
                for(int i=1;i<=99999;i++){
                    try {
                        Thread.sleep(50);
                        System.out.println("守护线程第"+i+"次运行.....");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("守护线程退出.....");
            }
        };
        //让daemonThread成为守护线程
        daemonThread.setDaemon(true);//这里必须在启动前设置,如果不设置,默认人是用户线程
        userThread.start();
        daemonThread.start();
        System.out.println("Main exit");
    }
}

プログラムの出力

Main exit
守护线程第1次运行.....
守护线程第2次运行.....
守护线程第3次运行.....
用户线程第1次运行.....
守护线程第4次运行.....
守护线程第5次运行.....
守护线程第6次运行.....
守护线程第7次运行.....
用户线程第2次运行.....
守护线程第8次运行.....
守护线程第9次运行.....
守护线程第10次运行.....
守护线程第11次运行.....
用户线程第3次运行.....
守护线程第12次运行.....
守护线程第13次运行.....
守护线程第14次运行.....
守护线程第15次运行.....
用户线程第4次运行.....
守护线程第16次运行.....
守护线程第17次运行.....
守护线程第18次运行.....
守护线程第19次运行.....
用户线程第5次运行.....
用户线程退出.....

Javaスレッドプールの使用のタイミング

結論: Javaスレッドプールはデフォルトのユーザー・スレッドによって作成され、メインスレッドが終了した後、プログラムの実行を継続します

import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10);

        executorService.scheduleWithFixedDelay(() -> {
            LocalDateTime localDateTime = LocalDateTime.now();
            System.out.println(localDateTime);
        },1, 1, TimeUnit.SECONDS);

        Thread.sleep(3000);
        System.out.println("Main Exit");
    }
}

プログラムの出力

2019-12-21T13:47:58.590
2019-12-21T13:47:59.592
Main Exit
2019-12-21T13:48:00.596
2019-12-21T13:48:01.601
2019-12-21T13:48:02.605
2019-12-21T13:48:03.610
2019-12-21T13:48:04.612
2019-12-21T13:48:05.615

デーモンスレッドプールのスレッドを使用します


import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10, new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setDaemon(true);
                return t;
            }
        });

        executorService.scheduleWithFixedDelay(() -> {
            LocalDateTime localDateTime = LocalDateTime.now();
            System.out.println(localDateTime);
        },1, 1, TimeUnit.SECONDS);

        Thread.sleep(10000);
        System.out.println("Main Exit");
    }
}
2019-12-21T13:45:53.966
2019-12-21T13:45:54.972
2019-12-21T13:45:55.975
2019-12-21T13:45:56.980
2019-12-21T13:45:57.985
2019-12-21T13:45:58.988
2019-12-21T13:45:59.993
2019-12-21T13:46:00.998
2019-12-21T13:46:02.003
Main Exit

Process finished with exit code 0

おすすめ

転載: www.cnblogs.com/textworld/p/12035069.html