Create java thread and get thread and stop thread according to thread name

Create java thread and get thread and stop thread according to thread name

1. Create, run, and suspend threads

public class MyThread extends Thread {
    
    
    @Override
    public void run() {
    
    
        try {
    
    
            // 睡眠三秒
            sleep(3000);
            // 逻辑代码
            System.out.println("逻辑代码......");
        } catch (InterruptedException e) {
    
    
            System.out.println("线程发生异常结束:" + Thread.currentThread().getName());
            System.out.println(e.getMessage());
        }
    }
    
    public static void main(String[] args) {
    
    
        // 创建线程
        MyThread myThread = new MyThread();
        // 启动线程
        myThread.start();
    }
}

Second, set and get the thread name

设置线程名称可以使用Thread类的如下方法:
1、构造方法:public Thread (Runnable Target,String name)
2、设置名字:public final void setName(String name)

// 创建线程 并设置线程名称
Thread myThread = new Thread("120");

// 创建线程
MyThread myThread = new MyThread();
// 设置线程名称
myThread.setName("110");

获取线程名称:
1、当前运行的线程名称:Thread.currentThread().getName()
2、取得名字:public final String getName()

System.out.println("获取当前运行的线程名称:" + Thread.currentThread().getName());
System.out.println("获取线程名称:" + myThread.getName());

Three, interrupt thread interrupt method

Interrupt() on the thread in sleep will throw an exception, follow the catch method, the run method will end normally, and the thread will be stopped safely.
You can design the way to stop the thread according to your own business process. It is not recommended to use the stop method, because there are data consistency problems, and the stop method has been expired by java.

kill thread

    public static void main(String[] args) {
    
    
        // 创建线程
        MyThread myThread = new MyThread();
        // 设置线程名称
        myThread.setName("110");
        System.out.println("获取线程名称:" + myThread.getName());
        // 启动线程
        myThread.start();
        // 中断线程
        myThread.interrupt();
    }

Fourth, get all threads

public class GetThreadName {
    
    

    public static void main(String[] args) {
    
    
        // 获取所有线程
        ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
        int noThreads = currentGroup.activeCount();
        Thread[] lstThreads = new Thread[noThreads];
        currentGroup.enumerate(lstThreads);
        System.out.println("现有线程个数:" + noThreads);
        // 遍历线程
        for (int i = 0; i < noThreads; i++) {
    
    
            String threadName = lstThreads[i].getName();
            System.out.println("第" + i + "个线程名为:" + threadName);
        }
    }

}

The output of the above code operation is (specifically, how many threads are running now):
Number of existing threads: 2
The name of the 0th thread: main
The name of the 1st thread: Monitor Ctrl-Break

5. Interrupt the thread according to the thread name

    public boolean killThreadByName(String name) {
    
    
        // 获取所有线程
        ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
        int noThreads = currentGroup.activeCount();
        Thread[] lstThreads = new Thread[noThreads];
        currentGroup.enumerate(lstThreads);
        System.out.println("现有线程个数:" + noThreads);

        for (int i = 0; i < noThreads; i++) {
    
    
            String threadName = lstThreads[i].getName();
            System.out.println("第" + i + "个线程名为:" + threadName);
            // 中断指定的线程
            if (threadName.equals(name)) {
    
    
                System.out.println("中断线程:" + name);
                lstThreads[i].interrupt();
                return true;
            }
        }
        return false;
    }

Guess you like

Origin blog.csdn.net/qq_42547733/article/details/128673076