thread management

1.1 Thread creation and operation

    In Java, we have 2 ways to create threads:

    1. By directly inheriting the thread class, and then overriding the run() method.

    2. Build a class that implements the Runnable interface, then create a thread class object and pass the Runnable object as a construction parameter

In the following example, we will use two methods to make a simple program that creates and runs 10 threads. Each thread can compute and output multiplication tables within 1-10.

    Implement the Runnable interface:

public class Calculator implements Runnable{

    private int number;

 

    public Calculator(int number) {

        this.number = number;

    }

 

    @Override

    public void run() {

        for(int i = 0; i < 10; i++) {

            System.out.printf("%s: %d * %d = %d\n",Thread.currentThread().getName(),number,i,i*number);

        }

    }

}

 

public class Main {

    public static void main(String[] args) {

 

    for(int i = 0; i < 10; i++) {

    Calculator cal = new Calculator(i);

    Thread thread = new Thread(cal);

    thread.start();

    }

}

 

Inherit the Thread class:

public class Calculator extends Thread{

    private int number;

 

    public Calculator(int number) {

    this.number = number;

    }

 

    @Override

    public void run() {

    for(int i = 0; i < 10; i++) {

         System.out.printf("%s: %d * %d = %d\n",Thread.currentThread().getName(),number,i,i*number);

    }

}

 

public class Main {

    public static void main(String[] args) {

        for(int i = 0; i < 10; i++) {

            Thread thread = new Calculator(i);

            thread.start();

        }

    }

}

    Every Java program has at least one thread of execution. When you run your program, the JVM runs the thread of execution responsible for calling the main() method.

    When the start() method of the Thread object is called, we create another thread of execution. After these start() methods are called, our program has multiple execution threads.

    The Java program ends when all thread execution ends (more specifically, when all non-daemon threads end). If the initial thread (the main thread executing the main() method) finishes running, other threads will continue to execute until the execution is complete. But if a thread calls System.exit() to instruct the program to terminate, then all threads will end execution.

    Creating an object of class Thread does not create a new thread of execution. Likewise, calling the run() method that implements the Runnable interface does not create a new thread of execution. A new thread of execution can only be created by calling the start() method.

 

1.2 Acquisition and setting of thread information

    Thread has some properties that hold information:

        ID: holds the unique identifier of the thread

        Name: saves the name of the thread

        Priority: saves the priority of the thread object. Priority goes from 0-10, 1 is the lowest and 10 is the highest

        Status:保存的线程的状态。在java中,一共有六种状态:new,runnable,running,blocked,waiting,time waiting,terminated

 

Thread thread = new Thread(runnable);

thread.getId()

thread.getName()

thread.setName

thread.getPriority()

threads[i].setPriority(Thread.MIN_PRIORITY);

thread.getState())

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326034549&siteId=291194637