线程调度的方法()

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 < 20; 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 < 100; 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 < 20; 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 < 20; 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 < 100; i++) {
            System.out.println("主线程执行了"+i+"次");
        }
    }
}

class TwoDog extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 100; 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 < 100; 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 < 100; i++) {
            System.out.println("第一个线程执行了"+i+"次");
        }
    }
}

class Second extends Thread{

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("第二个线程执行了"+i+"次");
        }
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值