美文网首页
Java 线程基本方法

Java 线程基本方法

作者: Tinyspot | 来源:发表于2022-09-02 07:58 被阅读0次

1. 获取线程名称

  • currentThread() 方法可返回代码段正在被哪个线程调用
  • public static native Thread currentThread()
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()

  • 当前线程(currentThread)主动休眠 N 毫秒
  • public static native void sleep(long millis) throws InterruptedException;
try {
    Thread.sleep(1000);
    TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
    e.printStackTrace();
}

2.2 yield()

  • 当前线程主动放弃时间片,回到就绪状态,竞争下一次时间片
  • public static native void 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()

  • 允许其他线程加入到当前线程中,会把当前线程阻塞,直到加入线程执行完毕,当前线程才会执行
  • join() 等待线程运行结束
  • public final void join() throws InterruptedException {}
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 优先级

  • 线程优先级为 1~10, 默认为 5,优先级越高,表示获取 CPU 机会越多
    1. 线程优先级会提示(hint)调度器优先调度该线程,但它仅仅是一个提示,调度器可以忽略它
    1. 如果 cpu 比较忙,那么优先级高的线程会获得更多的时间片,但 cpu 闲时,优先级几乎没作用
    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. 守护线程

  • thread.setDaemon(true); 将该线程标记为守护线程
  • 线程有两类
    • 用户线程(前台线程)
    • 守护线程(后台线程)
  • 只要有一个用户线程还在运行,Java 进程就不会结束
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 应用

  • 如果线程中所有前台线程都执行完毕了,后台线程会自动结束
  • JVM 关闭
    1,所有的非守护都已经运行结束。
    2,调用了System.exit方法。
    3,杀死JVM进程
    4,通过系统平台发送关闭信号(比如按Ctrl+C)
  • 应用:
  1. 垃圾回收器线程就是一种守护线程
  2. Tomcat 中的 Acceptor 和 Poller 线程都是守护线程,所以 Tomcat 接收到 shutdown 命令后,不会等待它们处理完当前请求

4. 不常用的方法

4.1 getId()

获取线程Id

4.2 dumpStack()

  • 获取当前线程的堆栈信息
  • public static void dumpStack() {}

相关文章

网友评论

      本文标题:Java 线程基本方法

      本文链接:https://www.haomeiwen.com/subject/mljsgrtx.html