Java线程的yield方法测试

来源:互联网 发布:淘宝主营类目在哪里看 编辑:程序博客网 时间:2024/06/05 04:36
/** * Created by Leon on 2017/5/22. * yield()让出cpu给别的线程工作 * 不常用 */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 s)    {        super(s);    }    public void run()    {        for (int i=1;i<=100;i++)        {            System.out.println(getName()+": "+i);            if (i%10==0)            {                yield();//整除10的时候让出cpu给别的线程工作            }        }    }}
原创粉丝点击