Java之线程调度、线程优先级、线程让位、线程合并

1、关于线程的调度

1.1常见的线程调度模型有哪些? .

  • 抢占式调度模型:
    哪个线程的优先级比较高,抢到的CPU时间片的概率就高一些/多一些。
    java采用的就是抢占式调度模型
  • 均分式调度模型:
    平均分配CPU时间片。每个线程占有的CPU时间片时间长度一样
    平均分配,一切平等。
    有一些编程语言,线程调度模型采用的是这种方式。

1.2 java中提供了哪些方法是和线程调度有关系的呢?

  • 1、实例方法:
    void setpriority (int newPriority)设置线程的优先级
    int getPriority()获取线程优先级
    最低优先级1
    默认优先级是5
    最高优先级10
  • 2、静态方法:
    static void yield()让位方法暂停当前正在执行的线程对象,并执行其他线程

yield()方法不是阻塞方法。让当前线程让位,让给其它线程使用。
yield()方法的执行会让当前线程从**“运行状态”回到“就绪状态”**。
注意:在回到就绪之后,有可能还会再次抢到CPU

  • 3、实例方法:
    void join()合并线程
class MyThread1 extends Thread {
    
    
		public void doSome(){
    
    
			Mythread2 t = new MyThread2();
			t,join();//当前线程进入阻塞,t线程执行,知道t线程结束。当前线程才可以继续
		} 	
}
class MyThread2 extends Thread{
    
    
}

2、线程优先级

int getPriority()获取线程优先级

public class ThreadTest09 {
    
    
    public static void main(String[] args) {
    
    
        System.out.println("最高优先级" + Thread.MAX_PRIORITY);
        System.out.println("最低优先级" + Thread.MIN_PRIORITY);
        System.out.println("默认优先级" + Thread.NORM_PRIORITY);

        //获取当前线程对象,获取当前线程的优先级
        Thread currentThread = Thread.currentThread();
        System.out.println(currentThread.getName() + "线程的默认优先级为:" + currentThread.getPriority());

        Thread t = new Thread(new MyRunnable5());
        t.setName("t");
        t.start();
    }
}

class MyRunnable5 implements Runnable{
    
    
    @Override
    public void run() {
    
    
        //获取线程优先级
        System.out.println(Thread.currentThread().getName() + "线程的默认优先级为" + Thread.currentThread().getPriority());
    }
}

在这里插入图片描述
void setpriority (int newPriority)设置线程的优先级

public class ThreadTest09 {
    
    
    public static void main(String[] args) {
    
    

        //设置主线程优先级为1
        Thread.currentThread().setPriority(1);
        //获取当前线程对象,获取当前线程的优先级
        Thread currentThread = Thread.currentThread();
        Thread t = new Thread(new MyRunnable5());
        t.setName("t");
        t.setPriority(10);
        t.start();

        //优先级较高的,只是抢到的CPU的时间片比较多一些。
        //大概率方向更偏向于优先级比较高的。
        for (int i = 0; i < 10000; i++) {
    
    
            System.out.println(Thread.currentThread().getName() + "-->" + i);
        }
    }
}

class MyRunnable5 implements Runnable{
    
    
    @Override
    public void run() {
    
    

        for (int i = 0; i < 10000; i++) {
    
    
            System.out.println(Thread.currentThread().getName() + "-->" + i);
        }
    }
}

在这里插入图片描述

3、线程让位

让位:值当前的线程暂停,回到就绪状态,让给其他线程。
静态方法:Thread.yield();

public class ThreadTest09 {
    
    
    public static void main(String[] args) {
    
    

        Thread t = new Thread(new MyRunnable5());
        t.setName("t");
        t.start();

        for (int i = 1; i < 10000; i++) {
    
    
            System.out.println(Thread.currentThread().getName() + "-->" + i);
        }
    }
}

class MyRunnable5 implements Runnable{
    
    
    @Override
    public void run() {
    
    
        for (int i = 1; i <= 10000; i++) {
    
    
            //每100个让位一次
            if (i % 100 == 0){
    
    
                Thread.yield();
            }
            System.out.println(Thread.currentThread().getName() + "-->" + i);
        }
    }
}

4、线程合并

线程的合并的含义就是将几个并行线程的线程合并为1个单线程执行。应用场景应该是一个线程必须等待另一个线程执行完毕才能使用join方法。

public class ThreadTest09 {
    
    
    public static void main(String[] args) {
    
    

        System.out.println("main begin");

        Thread t = new Thread(new MyRunnable5());
        t.setName("t");
        t.start();

        //合并线程
        try {
    
    
            t.join();//t合并到当前的线程中,当前线程受阻塞,t线程执行直到结束再继续执行main线程。
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        System.out.println("main over");
    }
}

class MyRunnable5 implements Runnable{
    
    
    @Override
    public void run() {
    
    
        for (int i = 0; i <= 100; i++) {
    
    
            System.out.println(Thread.currentThread().getName() + "-->" + i);
        }
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq2632246528/article/details/114834911