Java 线程基本方法

2022-09-02  本文已影响0人  Tinyspot

1. 获取线程名称

public static void main(String[] args) {
    System.out.println("main: " + Thread.currentThread().getName());
    MyThread thread = new MyThread();
    thread.start();
}

public class MyThread extends Thread {
    public MyThread() {
        System.out.println("MyThread currentThread: " + Thread.currentThread().getName());
        System.out.println("MyThread thisName: " + this.getName());
    }
    @Override
    public void run() {
        System.out.println("run currentThread:" + Thread.currentThread().getName());
        System.out.println("run thisName:" + this.getName());
    }
}

运行结果:
main: main
MyThread currentThread: main
MyThread thisName: Thread-0
run currentThread:Thread-0
run thisName:Thread-0
分析:MyThread 类的构造方法是 main 线程调用的,而 run() 方法是 JVM 自动调用的,线程名为 Thread-0

更复杂一点的情况

public static void main(String[] args) {
    MyThread myThread = new MyThread();
    // 将线程对象作为构造参数
    Thread thread = new Thread(myThread);
    thread.setName("thread-demo");
    thread.start();
}

运行结果
MyThread currentThread: main
MyThread thisName: Thread-0
run currentThread:thread-demo
run thisName:Thread-0
分析:
this.getName() 代表 MyThread 对象的名称,因 MyThread 对象的名称未设置,所以默认为 Thread-0

2. 基本方法

2.1 sleep()

try {
    Thread.sleep(1000);
    TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
    e.printStackTrace();
}

2.2 yield()

public class MyThread extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(this.getName() + ": " + i);
            Thread.yield();
        }
    }
}
MyThread thread = new MyThread();
MyThread thread2 = new MyThread();
thread.start();
thread2.start();

2.3 join()

public class MyThread extends Thread {
    @SneakyThrows
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println(this.getName() + ": " + i);
            Thread.sleep(100);
        }
    }
}
public static void main(String[] args) throws InterruptedException {
    MyThread thread = new MyThread();
    thread.start();

    // 加入当前线程,并阻塞当前线程,直到加入线程执行完毕
    thread.join();

    for (int i = 0; i < 20; i++) {
        System.out.println(Thread.currentThread().getName() + ": " + i);
        Thread.sleep(100);
    }
}

2.4 优先级

    MyThread thread = new MyThread("Thread1");
    MyThread thread2 = new MyThread("Thread2");
    MyThread thread3 = new MyThread("Thread3");

    thread.setPriority(1);
    thread3.setPriority(10);

    thread.start();
    thread2.start();
    thread3.start();

3. 守护线程

public static void main(String[] args) throws InterruptedException {
    Thread thread = new Thread(() -> {
        while (true) {
            System.out.println(Thread.currentThread().getName());
        }
    }, "t1");
    thread.setDaemon(true);
    thread.start();

    TimeUnit.SECONDS.sleep(1);
    System.out.println("main end");
}

3.1 应用

  1. 垃圾回收器线程就是一种守护线程
  2. Tomcat 中的 Acceptor 和 Poller 线程都是守护线程,所以 Tomcat 接收到 shutdown 命令后,不会等待它们处理完当前请求

4. 不常用的方法

4.1 getId()

获取线程Id

4.2 dumpStack()

上一篇下一篇

猜你喜欢

热点阅读