JAVAマルチスレッドyieldメソッド入門詳細

2545 ワード

package com.yys.thread;

/**
 * Created by yys on 2017/9/20.
 *    Thread yield  
 * yield     CPU           
 * join               CPU           
 *    (           )           1   if         CPU     2     2        if      CPU     1
 */
public class TestYield {
    public static void main(String[] args){
        MyThread3 t1 = new MyThread3("t1");
        MyThread3 t2 = new MyThread3("t2");
        t1.start();
        t2.start();
    }
}
class MyThread3 extends Thread{
    MyThread3(String name){
        super(name);
    }
    public void run(){
        for(int i=0;i<=100;i++){
            System.out.println(getName() + " : " + i);
            if(i % 10 == 0){
                yield();
            }
        }
    }
}