Common methods of java thread

Posted by dazz_club on Wed, 05 Jan 2022 15:22:36 +0100

Common methods of java thread

Common methods of Java threads

Common methods:

first group

method

1. setName / / set the thread name to be the same as the parameter name

2. getName / / returns the name of the thread

3. start / / start the thread to execute

4. run / / call the run method of the thread object

5. setPriority / / change thread priority

6. getpriority / / get the priority of the thread

7. sleep / / Hibernate (suspend) the currently executing thread within the specified number of milliseconds

8. interrupt / / interrupt the thread

Precautions and details

1. The bottom layer of start will create a new thread and call run. Run is a simple method call and will not start a new thread

2. Thread priority range

3. interrupt interrupts the thread and does not really end the thread. Therefore, it is generally used to interrupt the dormant thread

4. sleep: the static method of the thread to make the current thread sleep

case
package threaduse;

public class ThreadMethod01 {
    public static void main(String[] args) {
        Car car=new Car();
        Thread thread0=new Thread(car);
        Thread thread1=new Thread(car);
        thread0.start();
        thread1.start();
        for(int i=0;i<5;i++){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("hi"+i);
        }
        thread0.interrupt();//Interrupt thread sleep
        thread1.interrupt();//Interrupt thread sleep
    }


}
class Car implements Runnable{
    private static int num=0;
    private int count=0;
    @Override
    public void run() {
        Thread.currentThread().setName("car"+(num++));//Set thread name
        while(true){
            System.out.println(Thread.currentThread().getName()+"run"+(++count));//Get thread name
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(count==20){
                break;
            }
        }
    }
}

Group 2

method

1. yield: yield of threads. Give up cpu resources and let other threads execute, but the comity time is uncertain, so it may not succeed

2. join: queue jumping of threads. Once a thread that cuts the queue successfully, it must first complete all the tasks of the inserted thread.

Case:
package threaduse;

public class ThreadMothed02 {
    public static void main(String[] args) throws InterruptedException {
        Hi h1=new Hi();
        h1.start();
        //explain:
        // 1. Queue h1 in front of the main thread so that main will wait for h1 to finish executing
        // 2. If there is no join, it will be executed alternately
        h1.join();
        for(int i=1;i<20;i++){
            Thread.sleep(50);
            System.out.println("Zhang Sanfeng"+i);
        }
    }
}
class Hi extends Thread{
    @Override
    public void run() {
        for (int i = 1; i <20; i++) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("JoinThread----"+i);

        }
    }
}

practice

subject

1. The main thread outputs hi every 1s, 10 times in total

2. When outputting to hi 5, start a sub thread (Runnable is required), output Hello every 1s, and exit after the thread outputs 10 hello

3. The main thread continues to output hi until the thread exits.

4. Complete the code and realize thread queue jumping

code
package threaduse;

public class ThreadMethodExpercise {
    public static void main(String[] args) throws InterruptedException {
        Hello hello=new Hello();
        Thread thread=new Thread(hello);
        for (int i = 1; i <= 10; i++) {
            System.out.println("hi "+i);
            Thread.sleep(1000);
            if(i==5){
                thread.start();//Be careful not to write backwards here.
                thread.join();//You can try the reverse effect yourself.
            }
        }
    }
}
class Hello implements Runnable{
    @Override
    public void run() {
        for (int i = 1; i <= 10; i++) {
            System.out.println("hello "+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

User thread and daemon thread

introduce

**1.** User thread: also called worker thread, when the task of the thread is completed or the notification method ends

**2.** Daemon thread: it generally serves the worker thread when all user threads end. The daemon thread ends automatically

**3.** Common daemon threads: garbage collection mechanism

Daemon method settings:

T.setDaemon(true);

give an example:

package threaduse;

public class ThreadMethod03 {
    public static void main(String[] args) throws InterruptedException {
        T t=new T();
        Thread thread=new Thread(t);
        thread.setDaemon(true);//Set as daemon thread
        thread.start();
        for(int i=1;i<=10;i++){
            System.out.println("me too");
            Thread.sleep(1000);
            if(i==10){
                System.out.println("no");
            }
        }
    }
}
class T implements Runnable{
    @Override
    public void run() {
        while(true){
            System.out.println("i love you");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

Topics: Java Back-end