线程调度算法

1 . sleep() 在睡眠的时候 会释放cpu 让给其他线程执行, 即使没有其他线程 抢占cpu 也需要等待睡眠时间到了以后才能真正的指定.

package com.qf.demo3;

public class Test2 {

    public static void main(String[] args) {
        MyThread2 thread2 = new MyThread2();
        MyThead3 thead3 = new MyThead3();
        thread2.start();
        thead3.start();
    }
}

class MyThread2  extends Thread{

    @Override
    public void run() {
        for (int i = 0; i
            try {
                if(i>5){
                Thread.sleep(1000);// 可以让线程  睡眠  指定的毫秒数 
                // 在睡眠的时候 会释放cpu 让给其他线程执行
                // 即使没有其他线程 抢占cpu 也需要等待睡眠时间到了以后才能真正的指定

                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+”—>”+i);
        }
    }
}

class MyThead3 extends Thread{
    @Override
    public void run() {
        for (int i = 0; i
            System.out.println(“二狗跑了”+i+”圈”);
        }
    }
}

2 . yield(); 执行的时候 会 让出cpu , 但是 会立马同其他的线程抢占 cpu

package com.qf.demo3;

public class Test3 {

    public static void main(String[] args) {
        MyThrad4 myThrad4 = new MyThrad4(“白志凯”);
        MyThead5 myThead5 = new MyThead5();
        myThrad4.start();
        myThead5.start();

    }
}

class MyThrad4 extends Thread{
     public MyThrad4(String name) {
        super(name);
    }
    @Override
    public void run() {
        for (int i = 0; i
            Thread.yield();// 执行的时候 会  让出cpu , 但是 会立马同其他的线程抢占 cpu
            System.out.println(Thread.currentThread().getName()+”跑了”+i+”圈”);
        }
    }
}

class MyThead5 extends Thread{
    @Override
    public void run() {
        for (int i = 0; i
            System.out.println(“不傻的跑了”+i+”圈”);
        }
    }
}

3 . join()方法在哪个线程被调用,则就插入到哪个线程前面. 
此可实现插队 和 合并

package com.qf.demo3;
/**
* join 插队   , 合并

* @author Administrator
*
*/
public class Test4 {

    public static void main(String[] args) {
        TwoDog twoDog = new TwoDog();
        twoDog.start();

        try {
            twoDog.join();// 二狗想要插队  
            // 插队到当前线程前面
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        for (int i = 0; i
            System.out.println(“主线程执行了”+i+”次”);
        }
    }
}

class TwoDog extends Thread{
    @Override
    public void run() {
        for (int i = 0; i
            System.out.println(“英文二狗执行了”+i+”次”);
        }
    }
}

4 . 以下代码实现合并

package com.qf.demo3;
/**
* join方法  插队 合并
* 插队到的是当前线程前面(join方法在哪线程中被调用的  , 插队到哪个线程)
* @author Administrator
*
*/
public class Test5 {

    public static void main(String[] args) {
        First first = new First();
        first.start();

        try {
            first.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        for (int i = 0; i
            System.out.println(“主线程执行了”+i+”次”);
        }
    }
}

class First extends Thread{
    @Override
    public void run() {

        Second second = new Second();
        second.start();
        try {
            second.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        for (int i = 0; i
            System.out.println(“第一个线程执行了”+i+”次”);
        }
    }
}

class Second extends Thread{

    @Override
    public void run() {
        for (int i = 0; i
            System.out.println(“第二个线程执行了”+i+”次”);
        }
    }
}

 

更多学习资料可关注:itheimaGZ获取

文章知识点与官方知识档案匹配,可进一步学习相关知识算法技能树首页概览34296 人正在系统学习中

来源:码农的世界,你不懂

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年2月2日
下一篇 2020年2月2日

相关推荐