3.0 yield方法

yield()方法的作用是放弃当前CPU资源,将它让给其他任务去占用CPU执行时间,但是放弃的时间不确定,有可能刚刚放弃,马上又获得CPU时间片。
public class Run {

    @Test
    public void main() {
        MyThread myThread = new MyThread();
        myThread.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}



public class MyThread extends Thread {

    @Override
    public void run() {
        long begintime = System.currentTimeMillis();
        System.out.println(begintime);
        long count = 0;
        for (int i = 0; i < 5000; i++) {
             Thread.yield();
            count = count + (i + 1);
            System.out.println(count);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("耗时:" + (endTime - begintime) + "ms!");
    }
}

上述代码中,如果将Thread.yield();注释掉,线程所用时间将会缩短,原因是因为使用了yield方法,将cpu资源让出给了其他程序。那什么时候需要让出cpu资源呢?那就先说说线程的优先级

线程优先级,优先级高的得到CPU资源交多,也就是CPU优先执行优先级较高对象中的人物,设置优先级使用setPriority()。源码如下
   public final void setPriority(int newPriority) {
        ThreadGroup g;
    checkAccess();
    if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
        throw new IllegalArgumentException();
    }
    if((g = getThreadGroup()) != null) {
        if (newPriority > g.getMaxPriority()) {
        newPriority = g.getMaxPriority();
        }
        setPriority0(priority = newPriority);
        }
    }

其中MAX_PRIORITY=10,MIN_PRIORITY=1,类中预定义了3个优先级

 /**
     * The minimum priority that a thread can have. 
     */
    public final static int MIN_PRIORITY = 1;

   /**
     * The default priority that is assigned to a thread. 
     */
    public final static int NORM_PRIORITY = 5;

    /**
     * The maximum priority that a thread can have. 
     */
    public final static int MAX_PRIORITY = 10;
线程优先级的继承特征,比如A线程启动B线程,则B线程的优先级于A线程一样
public class Run {
    @Test
    public void main() {
        Thread.currentThread().setPriority(2);
        MyThread myThread = new MyThread();
        System.out.println("main:"+Thread.currentThread().getPriority());
        myThread.start();
    }
}

public class MyThread extends Thread {
    @Override
    public void run() {
        System.out.println("mythread:"+this.getPriority());
        MyThread01 thread01 = new MyThread01();
        thread01.start();
    }
}


public class MyThread01 extends Thread {
    @Override
    public void run() {
        System.out.println("thread 1 :" +this.getPriority());
    }
}

需要注意的是,设置优先级应该在创建线程之前,否则继承的将是原本默认的优先级值5

另外线程优先级越高获取cpu的优先级越高,并不是说要等优先级高的线程全部执行完才执行优先级低的,只是说优先级越高越先获取cpu资源而已

优先级具有随机性

优先级高的线程不一定每次都先执行完。

守护线程

Java线程中有两种线程,一种是用户线程,另一种是守护线程。守护线程是一种页数的线程,它的特征有“陪伴”的含义,当进程中没有非守护线程了,则守护线程自动销毁。典型的守护线程就是辣鸡回收线程,当进程中没有非守护线程了,则垃圾回收线程就没有存在的必要了,自动销毁。

public class Run {
    @Test
    public void main() {
        try {
            MyThread myThread = new MyThread();
            myThread.setDaemon(true);
            myThread.start();
            Thread.sleep(5000);
            System.out.println("离开,守护线程也将停止");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


public class MyThread extends Thread {
    int count=0;
    @Override
    public void run() {
        while(true){
            try {
                count++;
                System.out.println(count);
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

-------------------------------

1
2
3
4
5
离开,守护线程也将停止

你可能感兴趣的:(3.0 yield方法)