Java中Thread的常用方法介绍及线程的优先级介绍

3次阅读

共计 3504 个字符,预计需要花费 9 分钟才能阅读完成。

索引
一、Thread 的常用方法
1.run() 方法
2.start() 方法
3.currrentThread() 方法,返回当前执行代码的线程,该方法为静态方法,直接通过 Thread 类名调用
4.getName() 方法,获取当前线程的名字
5.setName() 方法,设置当前线程名字
6.yield() 方法,释放当前线程的操作
7.join() 方法
8.sleep() 方法
9.isAlive() 方法
二、线程的优先级
3 个优先级静态常量
如何获取和设置当前线程的优先级

一、Thread 的常用方法

1.run()方法

作用:子类通常需要重写 Thread 类中的 run()方法,run()中写你想让该线程执行的代码。

//MyThread 继承 Thread
class MyThread extends Thread{//1. 子类 MyThread 重写 Thread 类中的 run()方法
    @Override
    public void run() {for(int i = 2;i <= 10;i++){System.out.println(i);
        }
    }
}

2.start()方法

作用:启动当前线程,并调用当前线程中的 run()方法

class MyThread extends Thread{
    @Override
    public void run() {for(int i = 2;i <= 10;i++){System.out.println(i);
        }
    }
}
public class ThreadTest{public static void main(String args[]){MyThread m1 = new MyThread();
        //2. 调用 m1 的 start()方法
        m1.start();}
}

输出结果:
0

3.currrentThread()方法,返回当前执行代码的线程,该方法为静态方法,直接通过 Thread 类名调用

一般要操控某个线程,先通过该方法 currentThread()获取到该线程,然后再访问该线程里的方法,对该线程进行操作。例如:我想获取当前线程的名字,代码如下:

public class ThreadTest{public static void main(String args[]){
        // 获取主线程的名字
        // 先通过 Thread.currentThread()方法返回当前线程,然后调用该线程里的 getName()方法,获取当前线程的名字
         System.out.println("当前线程名字为:" + Thread.currentThread().getName());
    }
}

4.getName()方法,获取当前线程的名字

具体代码参见第 3 点

5.setName()方法,设置当前线程名字

public class ThreadTest{public static void main(String args[]){// 先通过 Thread.currentThread()获取当前线程
        // 调用当前线程的 setName()方法,为当前线程设置名字。Thread.currentThread().setName("主线程:");
        System.out.println("当前线程名字为:" + Thread.currentThread().getName());
    }
}

6.yield()方法,释放当前线程的操作

执行 yield()方法,cpu 会释放当前线程,去执行其它的线程,过后还是会继续执行该线程,具体什么时候执行该线程,看 cpu 心情。

class MyThread extends Thread{
    @Override
    public void run() {for(int i = 1;i <= 10;i++){System.out.println(i);
            if(i % 2 == 0){
                // 写法一
                Thread.currentThread().yield();
                // 写法二:由于该类继承了 Thread 类,所以该子类拥有 yield()方法, 可以直接使用 yield()来释放当前线程
                //yield();}
        }
        
    }
}

7.join()方法

如果在线程 a 中调用了线程 b 的 join()方法,此时线程 a 就会进入阻塞状态,直到线程 b 完全执行完以后,线程 a 才会结束阻塞状态,由于 join 方法中会抛出异常,所以我们还需要 try-catch 来捕捉处理异常

class MyThread extends Thread{
    @Override
    public void run() {for(int i = 1;i <= 10;i++){System.out.println(getName() + i);
        }

    }
}
public class Test{public static void main(String[] args) {Thread.currentThread().setName("主线程:");
        MyThread t1 = new MyThread();
        t1.setName("子线程:");
        t1.start();
        for(int i = 1;i <= 10;i++){if(i == 5){
                try {// 在主线程中调用了 t1 的 join()方法,主线程进入阻塞状态,直到 t1 执行完毕后,才继续执行主线程
                    t1.join();} catch (InterruptedException e) {e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + i);
        }

    }
}

输出结果:

8.sleep()方法

sleep()方法内的形参为 long millitime, 调用该方法会让当前线程“睡眠”指定的 millitime(毫秒),在指定的 millitime 毫秒的时间内,当前的线程是阻塞状态。

public class Test {public static void main(String[] args) {Thread.currentThread().setName("主线程:");
        try {for (int i = 1; i <= 10; i++) {
                // 让该线程睡眠 0.5 秒
                Thread.currentThread().sleep(500);
                System.out.println(Thread.currentThread().getName() + i);
            }
        } catch (InterruptedException e) {e.printStackTrace();
        }

    }
}

9.isAlive()方法

isAlive()方法用来判断当前线程是否存活,如果当前线程存活,返回 true, 如果消亡,返回 false

public class Test {public static void main(String[] args) {for (int i = 1; i <= 10; i++) {System.out.println(Thread.currentThread().getName() + ":" + i);
        }
        // 输出当前的线程是否存活
        // 由于判断的是主线程是否存活,所以主线程执行 isAlive()方法时,肯定是存活的
        System.out.println(Thread.currentThread().isAlive());
    }
}

运行结果:
0

二、线程的优先级

3 个优先级静态常量

MAX_PRIORITY:10(最高优先级)
MIN_PRIORITY:1(最低优先级)
NORM_PRIORITY:5(默认优先级)

如何获取和设置当前线程的优先级

获取当前线程的优先级:getPriority()
设置当前线程的优先级:setPriority(int p),p 的取值范围为 1~10
说明:高优先级的线程从概率上来讲会有较高的概率抢占低优先级线程的 cup 执行权,并不意味着只有当高优先级的线程执行完之后,低优先级的线程才执行。

class MyThread extends Thread {
    @Override
    public void run() {for (int i = 1; i <= 10; i++) {System.out.println(getName() + i);
        }

    }
}

public class Test {public static void main(String[] args) {Thread.currentThread().setName("主线程:");
        MyThread t1 = new MyThread();
        t1.setName("子线程 1:");
        t1.start();
        // 获取线程的优先级
        System.out.println("修改前子线程的优先级为:" + t1.getPriority());
        // 设置线程的优先级
        t1.setPriority(10);
        System.out.println("修改后子线程的优先级为:" + t1.getPriority());
    }
}
正文完
 0