“This is the 28th day of my participation in the August Gwen Challenge.

Written in the preface

This article focuses on common methods for Thread. If you are interested, you can take a look at some of my insights into multithreading through the portal. (Portal 1)(Portal 2)

Common methods of Thread

Thread Obtain the name of the Thread

Each thread has an identity name, and multiple threads can have the same name. If a thread is created without an identity name, a new name is generated for it.

There are two ways to get the name of a thread:

  • 1. Use the Thread method getName().

    String getName() returns the name of the thread.

  • 2. You can obtain the name of the thread that is executing by using the method getName()

    Static Thread currentThread () returns a reference to the Thread object currently executing.

Let’s look at a concrete example:

Use the getName method directly

String name = getName();
Copy the code

Use the currentThread method to get the currently executing thread

Thread thread = currentThread();
String name1 = thread.getName();
Copy the code

The above three statements can be written as one

Chain programming

Copy the code

Thread Common methods: Set the Thread name

There are also two ways to set the name of the thread:

  • 1. Use the Thread method setName.

    Void setName(String name) Changes the thread name to be the same as the parameter name.

  • 2. Create a constructor with an argument that passes the name of the money thread; Call the parent class’s parameterized constructor, pass the Thread name to the parent class, and let the parent class (Thread) give the child Thread a name

    Thread(string name) Allocates new Thread objects.

Look at a concrete example:

Start by subclassing Thread and overriding the run method

Public class MyThread extends Thread{public MyThread(){} public MyThread(String name){public MyThread(String name) super(name); @override public void run() {system.out.println (thread.currentThread ().getName()); }}Copy the code

Create objects in the test class

public class MyThreadTest { public static void main(String[] args) { MyThread mt = new MyThread(); // Set thread name mt.setName(" xiao Ming "); mt.start(); MyThread mt2 = new MyThread(" red "); mt2.start(); }}Copy the code

Thread common method: sleep

public static void sleep(Long millis):

Causes the currently executing thread to pause (temporarily suspend execution) for the specified number of milliseconds. When the number of milliseconds is up, the thread resumes execution

Let’s look at the sleep method through a small case;

Requirements: Analog stopwatch: print a number every second, from 1 to 60.

for (int i = 1; i <= 60; i++) { System.out.println(i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }}Copy the code

Write in the last

Thread Thread Thread Thread Thread Thread Thread Thread Thread Thread Thread Thread Thread

If the above content is not correct, welcome to dig friends to criticize.