Daemon Thread en Java

Mohammad Irfan 12 octobre 2023
  1. Créer un thread de démon en utilisant la méthode setDaemon() en Java
  2. Créer un thread de démon en utilisant la méthode setDaemon() en Java
  3. Créer un thread de démon en utilisant la méthode setDaemon() en Java
Daemon Thread en Java

Ce didacticiel présentera ce qu’est le thread démon et comment créer un thread démon en Java.

En Java, un thread démon est un thread spécial prenant en charge le thread d’arrière-plan pour d’autres threads. Java utilise ces threads à des fins spéciales pour les threads utilisateur et le garbage collection, etc.

Pour créer un thread démon, Java fournit la méthode setDaemon() qui prend un argument booléen. La méthode isDaemon() permet de vérifier si le thread en cours d’exécution est un thread Daemon ou non.

Créer un thread de démon en utilisant la méthode setDaemon() en Java

Dans cet exemple, nous utilisons la méthode setDaemon() de la classe Thread pour créer un thread démon. Nous créons trois threads, et deux d’entre eux sont définis comme threads démon. Nous utilisons la méthode isDaemon() pour vérifier si le thread en cours d’exécution est un thread démon ou non. Voir l’exemple ci-dessous.

public class SimpleTesting extends Thread {
  public MainClass(String name) {
    super(name);
  }
  public void run() {
    boolean isDaemon = Thread.currentThread().isDaemon();
    System.out.println("Is this daemon thread: " + isDaemon);
  }
  public static void main(String[] args) {
    MainClass thread1 = new MainClass("Thread 1");
    MainClass thread2 = new MainClass("Thread 2");
    MainClass thread3 = new MainClass("Thread 3");
    thread1.setDaemon(true);
    thread1.start();
    thread2.start();
    thread3.setDaemon(true);
    thread3.start();
  }
}

Production:

Is this daemon thread : true Is this daemon thread : false Is this daemon thread : true

Créer un thread de démon en utilisant la méthode setDaemon() en Java

Le thread démon est le thread de faible priorité et s’exécute toujours derrière les threads utilisateur. Nous pouvons voir la priorité d’un thread en utilisant la méthode getPriority() qui renvoie une valeur entière. Nous utilisons la méthode getName() pour récupérer le nom du thread en cours d’exécution. Voir l’exemple ci-dessous.

public class SimpleTesting extends Thread {
  public MainClass(String name) {
    super(name);
  }
  public void run() {
    boolean isDaemon = Thread.currentThread().isDaemon();
    System.out.println("Is this daemon thread: " + isDaemon + ", "
        + Thread.currentThread().getName() + ", " + Thread.currentThread().getPriority());
  }
  public static void main(String[] args) {
    MainClass thread1 = new MainClass("Thread 1");
    MainClass thread2 = new MainClass("Thread 2");
    MainClass thread3 = new MainClass("Thread 3");
    thread1.setDaemon(true);
    thread1.start();
    thread2.start();
    thread3.setDaemon(true);
    thread3.start();
  }
}

Production:

Is this daemon thread: true, Thread 1, 5
Is this daemon thread: false, Thread 2, 5
Is this daemon thread: true, Thread 3, 5

Créer un thread de démon en utilisant la méthode setDaemon() en Java

Nous démarrons le thread après avoir mis une valeur à la méthode setDaemon() lors de la création d’un thread démon. C’est essentiel car si nous définissons un thread déjà en cours d’exécution comme démon, il lèvera une exception. Nous appelons la méthode start() avec thread1 puis appelons setDaemon(), ce qui n’est pas autorisé. Voir l’exemple ci-dessous.

public class SimpleTesting extends Thread {
  public MainClass(String name) {
    super(name);
  }
  public void run() {
    boolean isDaemon = Thread.currentThread().isDaemon();
    System.out.println("Is this daemon thread: " + isDaemon + ", "
        + Thread.currentThread().getName() + ", " + Thread.currentThread().getPriority());
  }
  public static void main(String[] args) {
    MainClass thread1 = new MainClass("Thread 1");
    MainClass thread2 = new MainClass("Thread 2");
    MainClass thread3 = new MainClass("Thread 3");
    thread1.start();
    thread1.setDaemon(true);
    thread2.start();
    thread3.setDaemon(true);
    thread3.start();
  }
}

Production:

Is this daemon thread: false, Thread 1, 5
Exception in thread "main" java.lang.IllegalThreadStateException

Article connexe - Java Thread