Understanding multithreading -1.13 yield before and after use, Calculate time difference

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.