标签:
这一章节我们讨论yield的使用。
1.yield的作用
当我们使用这个方法的时候,我们会让出cup的控制时间,让其他线程计算
2.代码清单
package com.ray.deepintothread.ch01.topic_13;
public class YieldSample {
public static void main(String[] args) throws InterruptedException {
ThreadOne threadOne = new ThreadOne();
Thread thread = new Thread(threadOne);
thread.start();
}
}
class ThreadOne implements Runnable {
@Override
public void run() {
long startTime = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
Thread.yield();
}
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime);
}
}
192
当我们注释了Thread.yield()这一句,输出是:
1
对比两个输出我们可以看见,由于使用yield方法让出了cup的时间片段,因此计算时间比独占线程的要多得多。
总结:这一章节展示了使用yield方法前后的计算时间的差别
我的github:https://github.com/raylee2015/DeepIntoThread
从头认识多线程-1.13 yield在使用前后,计算时间的差别
标签:
原文地址:http://blog.csdn.net/raylee2007/article/details/51178434