在Java中命名线程并获取当前线程的名称

作者: Arvin Chen 分类: Java 来源: Break易站(www.breakyizhan.com)

在Java中命名一个线程

在Java中,每个线程都有一个名称,即Thread-0,Thread-1,Thread-2,... .so。Java提供了一些更改线程名称的方法。基本上有两种方法来设置线程名称。这两种方法都在java.lang.Thread类中定义。

  1. 直接设置线程的名称:我们可以在创建线程时设置线程名称,并将线程的名称作为参数传递。
    // Java program to illustrate 
    // how to set the name
    // of a thread at time of
    // thread creation
    import java.io.*;
    
    // we can create thread by creating the
    // objects of that class
    class ThreadNaming extends Thread
    {
         
        ThreadNaming(String name)
        {
            // call to constructor of
            // the Thread class.
            super(name);
        }
    
        @Override
        public void run()
        {
            System.out.println("Thread is running.....");
        }
    }
    
    class GFG
    {
        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());
             
            t1.start();
            t2.start();
        }
    }
    

    输出:

    Thread 1: geek1
    Thread 2: geek2
    Thread is running.....
    Thread is running.....
  2. 使用setName(String threadName)方法:我们可以通过调用该线程对象上的setName方法来设置(更改)线程的名称。
    句法:

     public final void setName(String name)
    //它将更改线程的名称。
    
    // Java program to illustrate
    // how to get and change the
    // name of a thread.
    import java.io.*;
    
    // we can create thread by creating the
    // objects of that class.
    class ThreadNaming extends Thread
    {
        
        @Override
        public void run()
        {
            System.out.println("Thread is running.....");
        }
    }
    
    class GFG
    {
        public static void main (String[] args)
        {
            // creating two threads
            ThreadNaming t1 = new ThreadNaming();
            ThreadNaming t2 = new ThreadNaming();
            
            // getting the above created threads names.
            System.out.println("Thread 1: " + t1.getName());
            System.out.println("Thread 2: " + t2.getName());
            
            t1.start();
            t2.start();
            
            // Now changing the name of threads.
            t1.setName("geeksforgeeks");
            t2.setName("geeksquiz");
            
            // again getting the new names
            // of the threads.
            System.out.println("Thread names after changing the "+
            "thread names");
            System.out.println("New Thread 1 name:  " + t1.getName());
            System.out.println("New Thread 2 name: " + t2.getName());
            
        }
    }
    

    输出:

    Thread 1: Thread-0
    Thread 2: Thread-1
    Thread names after changing the thread names
    New Thread 1 name:  geeksforgeeks
    New Thread 2 name: geeksquiz
    Thread is running.....
    Thread is running.....

在Java中获取当前线程的名称

我们可以在创建线程时获取当前线程名称,并将线程的名称作为参数传递。
句法:

 public static Thread currentThread() 
//它在java.langThread类中定义。 
//它返回对当前正在执行的线程的引用。
// Java program to illustrate
// how to get name of current
// executing thread.
import java.io.*;

// we can create thread by creating the
// objects of that class
class ThreadNaming extends Thread
{
    
    @Override
    public void run()
    {
        // getting the current thread 's name.
        System.out.println("Fetching current thread name..");
        System.out.println(Thread.currentThread().getName());
    }
}

class GFG
{
    public static void main (String[] args)
    {
        // creating two threads
        ThreadNaming t1 = new ThreadNaming();
        ThreadNaming t2 = new ThreadNaming();
        
        t1.start();
        t2.start();
    }
}

输出:

Fetching current thread name..
Thread-0
Fetching current thread name..
Thread-1
  •   本文标题:在Java中命名线程并获取当前线程的名称 - Break易站
    转载请保留页面地址:https://www.breakyizhan.com/java/4961.html
    扫描二维码添加微信 
  • ,领取淘宝优惠券,淘宝购物更优惠。现在添加微信,还可以领取机械键盘优惠券!添加微信后,分享淘宝选中的机械键盘给淘宝机器人即可领取!
    支持我们,就用微信淘宝!

    发表笔记

    电子邮件地址不会被公开。 必填项已用*标注

    更多阅读