java基础语法(三十五)线程和进程(二)——线程的调度以及优先级

线程的调度

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

1.2.java中提供了哪些方法是和线程调度有关系的呢?
        实例方法:
            vold setPriority (int newPriority) 设置线程的优先级
            int getPriority() 获取线程优先级
            最低优先级1
            默认优先级是5
            最高优先级10
            优先级比较高的获取CPU时间片可能会多一些。(但也不完全是,大概率是多的。) .
        静态方法:
            static void yield() 让位方法
            暂停当前正在执行的线程对象,并执行其他线程
            yield()方法不是阻塞方法。让当前线程让位,让给其它线程使用.
            yield()方法的执行会让当前线程从"运行状态"回到"就绪状态"。
            注意:在回到就绪之后,有可能还会再次抢到。

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

线程的优先级

public class ThreadTest11 {
    public static void main(String[] args) {
        //设置主线程的优先级为1
        Thread.currentThread().setPriority(1);
        /*
        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.setPriority(10);
        t.setName("t");
        t.start();

        //优先级较高的,只是捡到的CPU时间片相对多一些。
        for (int i=0;i<100;i++){
            System.out.println(Thread.currentThread().getName()+"--->"+i);
        }
    }
}

class MyRunnable5 implements Runnable{

    @Override
    public void run() {
        //获取当前线程对象,获取当前线程的优先级
        //System.out.println(Thread.currentThread().getName()+"线程的默认优先级是:"+Thread.currentThread().getPriority());
        for (int i=0;i<100;i++){
            System.out.println(Thread.currentThread().getName()+"--->"+i);
        }
    }
}

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

public class ThreadTest12 {
    public static void main(String[] args) {
        Thread t=new Thread(new MyRunnable6());
        t.setName("t");
        t.start();
        for (int i=0;i<10000;i++){
            System.out.println(Thread.currentThread().getName()+"--->"+i);
        }
    }


}

class MyRunnable6 implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i <= 10000; i++) {
            //每100个让位一次
            if(i%100==0)
            {
                Thread.yield();//当前线程暂停一下,让给主线程
            }
            System.out.println(Thread.currentThread().getName() + "--->" + i);
        }
    }
}

线程合并

public class ThreadTest13 {
    public static void main(String[] args) {
        System.out.println("main begin");
        Thread t=new Thread(new MyRunnable7());
        t.setName("t");
        t.start();

        //合并线程
        try {
            t.join();//t合并到当前线程当中,当前线程受阻塞,t线程执行直到结束
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("main over");
    }
}
class MyRunnable7 implements Runnable{
    @Override
    public void run() {
        for (int i=0;i<100;i++){
            System.out.println(Thread.currentThread().getName()+"--->"+i);
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值