Java线程中的常用方法和线程优先级的设置


Java线程中的常用方法和线程优先级的设置

Thread类中的

  1. **start()**:启动当前线程,调用当前线程的run()

  2. run(): 通常需要重写Thread类中的此方法,将线程要执行的操作声明在此方法中

  3. currentThread(): 静态方法,返回执行当前代码的线程

  4. getName(): 获取当前线程的名字

  5. setName(): 设置当前线程的名字

    代码示例1

    public class ThreadMethodTest {
    
       public static void main(String[] args) {
           HelloThread h1 = new HelloThread();
           h1.setName("线程一");
           h1.start();
    
           //给主线程命名
           Thread.currentThread().setName("主线程");
           for (int i = 0; i < 100; i++) {
               if (i % 2 == 0){
                   System.out.println(Thread.currentThread().getName()+":"+i);
               }
           }
       }
    }
    class HelloThread extends Thread{
       @Override
       public void run() {
           for (int i = 0; i < 100; i++) {
               if (i % 2 == 0){
                   System.out.println(Thread.currentThread().getName()+":"+i);
               }
           }
       }
    }

    代码示例2:继承父类Thread的构造方法设置线程名

    public class ThreadMethodTest {
    
       public static void main(String[] args) {
           HelloThread h1 = new HelloThread("线程一");
    //        h1.setName("线程一");
           h1.start();
       }
    }
    class HelloThread extends Thread{
    
       public HelloThread(String name) {
           super(name);
       }
       @Override
       public void run() {
           for (int i = 0; i < 100; i++) {
               if (i % 2 == 0){
                   System.out.println(Thread.currentThread().getName()+":"+i);
               }
           }
       }
    }
  6. yield(): 释放当前cpu的执行权,实际中无法保证yield()达到让步目的,因为让步的线程还有可能被线程调度程序再次选中

    public class ThreadMethodTest {
    
       public static void main(String[] args) {
           HelloThread h1 = new HelloThread("线程一");
    //        h1.setName("线程一");
           h1.start();
    
           for (int i = 0; i < 100; i++) {
               if (i % 2 == 0){
                   System.out.println(Thread.currentThread().getName()+":"+i);
               }
           }
       }
    }
    class HelloThread extends Thread{
    
       public HelloThread(String name) {
           super(name);
       }
    
       @Override
       public void run() {
           for (int i = 0; i < 100; i++) {
               if (i % 2 == 0){
                   System.out.println(Thread.currentThread().getName()+":"+i);
               }
               if (i % 20 == 0){
                   Thread.yield();
               }
           }
       }
    }
  7. join(): 在线程a中调用线程b的join(),此时线程a进入阻塞状态,直到线程b完全执行完以后,线程a才结束阻塞状态

    public class ThreadMethodTest {
    
       public static void main(String[] args) throws InterruptedException {
           HelloThread h1 = new HelloThread("线程一");
    //        h1.setName("线程一");
           h1.start();
    
           for (int i = 0; i < 100; i++) {
               if (i % 2 == 0){
                   System.out.println(Thread.currentThread().getName()+":"+i);
               }
               if (i == 20){
                   h1.join();
               }
           }
       }
    }
    
    class HelloThread extends Thread{
    
       public HelloThread(String name) {
           super(name);
       }
    
       @Override
       public void run() {
           for (int i = 0; i < 100; i++) {
               if (i % 2 == 0){
                   System.out.println(Thread.currentThread().getName()+":"+i);
               }
               if (i % 20 == 0){
                   Thread.yield();
               }
           }
       }
    }
    线程一:6
    main:18
    线程一:8
    main:20
    线程一:10
    线程一:12
    线程一:14
    。。。
    线程一:94
    线程一:96
    线程一:98
    main:22
    main:24
    main:26
    main:28
    。。。

    在主线程i==20时,执行join(),主线程进入阻塞状态,当分线程执行完后,主线程才结束阻塞状态继续执行

  8. stop(): 已过时。当执行此方法时,强制结束当前线程

  9. sleep(long millitime): 让当前线程“睡眠”指定的millitime毫秒。在指定的millitime毫秒时间内,当前线程是阻塞状态

    public class ThreadMethodTest {
    
       public static void main(String[] args) throws InterruptedException {
           HelloThread h1 = new HelloThread("线程一");
    //        h1.setName("线程一");
           h1.start();
    
           for (int i = 0; i < 100; i++) {
               if (i % 2 == 0){
                   System.out.println(Thread.currentThread().getName()+":"+i);
               }
           }
       }
    }
    
    class HelloThread extends Thread{
    
       public HelloThread(String name) {
           super(name);
       }
    
       @Override
       public void run() {
           for (int i = 0; i < 100; i++) {
               if (i % 2 == 0){
                   try {
                       sleep(1000);
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
                   System.out.println(Thread.currentThread().getName()+":"+i);
               }
           }
       }
    }
  10. isAlive():判断当前线程是否存活

public class ThreadMethodTest {

    public static void main(String[] args) throws InterruptedException {
        HelloThread h1 = new HelloThread("线程一");
//        h1.setName("线程一");
        h1.start();

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

        System.out.println(h1.isAlive());
    }
}

class HelloThread extends Thread{

    public HelloThread(String name) {
        super(name);
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0){
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+":"+i);
            }
        }
    }
}

线程优先级的设置

线程的调度

调度策略

  • 分时调度

    分时调度模型是指让所有的线程轮流获得cpu的使用权,并且平均分配每个线程占用的CPU的时间片这个也比较好理解。image-20220729183929467

  • 抢占式调度:高优先级的线程抢占CPU

java虚拟机采用抢占式调度模型,是指优先让可运行池中优先级高的线程占用CPU,如果可运行池中的线程优先级相同,那么就随机选择一个线程,使其占用CPU。处于运行状态的线程会一直运行,直至它不得不放弃CPU。

线程优先级

线程优先级等级

  • MAX_PRIORITY: 10 最高优先级
  • MIN_PRIORITY: 1 最低优先级
  • NORM_PRIORITY: 5 分配给线程的默认优先级

方法

  • getPriority(): 返回当前线程优先值
  • setPriority(int newProority): 改变线程的优先级

说明

线程创建时继承父线程优先级,低优先级知识获得调度的概率低,并非一定是在高优先级线程之后调用

class HelloThread extends Thread{

    public HelloThread(String name) {
        super(name);
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0){
                System.out.println(getName()+":"+getPriority()+":"+i);
            }
        }
    }
}

image-20220729190628272

可以看到没有设置优先级,线程的默认优先级是5,通过 setPriority()设置线程HelloThread优先级为最高,将主线程优先级设最低

public class ThreadMethodTest {

    public static void main(String[] args) throws InterruptedException {
        HelloThread h1 = new HelloThread("线程一");

        //设置分线程的优先级
        h1.setPriority(Thread.MAX_PRIORITY);
        // 设置主线程优先级
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        h1.start();

        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0){
                System.out.println(Thread.currentThread().getName()+":"+Thread.currentThread().getPriority()+":"+i);
            }
        }
    }
}

Author: qwq小小舒
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source qwq小小舒 !
  TOC