Nombrar un hilo y obtener el nombre del hilo actual en Java

Thread puede ser referido como un proceso ligero. Thread utiliza menos recursos para crear y existir en el proceso; subproceso comparte recursos de proceso. El hilo principal de Java es el hilo que se inicia cuando se inicia el programa. Ahora discutamos el concepto excéntrico de cómo podemos nombrar un hilo. 

Métodos:   hay dos formas en las que podemos establecer el nombre, ya sea directa o indirectamente, a través del cual estaremos mirando.

Java

// Java Program Illustrating How to Set the name
// of Thread at time of Creation
  
// Importing I/O classes from java.io package
import java.io.*;
  
// Class 1
// Helper class
class ThreadNaming extends Thread {
  
    // Parameterized constructor
    ThreadNaming(String name)
    {
        // Call to constructor of
        // the Thread class as super keyword
        // refers to parent class
        super(name);
    }
  
    // run() method for thread
    @Override public void run()
    {
        // Print statement when thread is called inside
        // main()
        System.out.println("Thread is running.....");
    }
}
  
// Class 2
// Main class
class GFG {
  
    // main driver method
    public static void main(String[] args)
    {
  
        // Creating two threads
        ThreadNaming t1 = new ThreadNaming("geek1");
        ThreadNaming t2 = new ThreadNaming("geek2");
  
        // Getting the above created threads names.
        System.out.println("Thread 1: " + t1.getName());
        System.out.println("Thread 2: " + t2.getName());
  
        // Starting threads using start() method
        t1.start();
        t2.start();
    }
}

Java

// Java Program Illustrating How to Get and Change the
// Name of a Thread
  
// Importing input output classes
import java.io.*;
  
// Class 1
// Helper class extending Thread class
class ThreadNaming extends Thread {
  
    // run() method for thread which is called
    // as soon as start() is called over threads
    @Override public void run()
    {
  
        // Print statement when run() is called over thread
        System.out.println("Thread is running.....");
    }
}
  
// Class 2
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating two threads via above class
        // as it is extending Thread class
        ThreadNaming t1 = new ThreadNaming();
        ThreadNaming t2 = new ThreadNaming();
  
        // Fetching the above created threads names
        // using getName() method
        System.out.println("Thread 1: " + t1.getName());
        System.out.println("Thread 2: " + t2.getName());
  
        // Starting threads using start() method
        t1.start();
        t2.start();
  
        // Now changing the name of threads
        t1.setName("geeksforgeeks");
        t2.setName("geeksquiz");
  
        // Again getting the new names of threads
        System.out.println(
            "Thread names after changing the "
            + "thread names");
  
        // Printing the above names
        System.out.println("New Thread 1 name:  "
                           + t1.getName());
        System.out.println("New Thread 2 name: "
                           + t2.getName());
    }
}

Java

// Java program to Illustrate How to Get Name of
// Current Executing thread
// Using getName() Method
  
// Importing reqiored I/O classes
import java.io.*;
  
// Class 1
// Helper class extending to Thread class
class ThreadNaming extends Thread {
  
    // run() method for this thread
    @Override public void run()
    {
        // Display message
        System.out.println(
            "Fetching current thread name..");
  
        // Getting the current thread name
        // using getname() method
        System.out.println(
            Thread.currentThread().getName());
    }
}
  
// Class 2
// Main class
class GFG {
  
    // Main method driver
    public static void main(String[] args)
    {
  
        // Creating two threads inside main() method
        ThreadNaming t1 = new ThreadNaming();
        ThreadNaming t2 = new ThreadNaming();
  
        // Starting threads using start() method which
        // automatically calls run() method
        t1.start();
        t2.start();
    }
}

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *