In this chapter we discuss the use of yield.
The role of 1.yield
When we use this method, we give the Cup control time and let the other threads calculate
2. Code Listing
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 {@Overridepublic void run () {Long startTime = System.currenttimemillis (); for (int i = 0; i < 1000000; i++) {Thread.yield ();} Long endTime = System.currenttimemillis (); System.out.println (Endtime-starttime);}}
Output:
192
When we comment on the Thread.yield (), the output is:
1
Compare two outputs we can see that because the yield method yields the cup's time fragment, the computation time is much more than the exclusive thread.
Summary: This section shows the difference in computational time before and after using the yield method
My github:https://github.com/raylee2015/deepintothread.
Understanding multithreading -1.13 yield before and after use, Calculate time difference