DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Data
  4. yield(), sleep(0), wait(0,1) and parkNanos(1)

yield(), sleep(0), wait(0,1) and parkNanos(1)

Peter Lawrey user avatar by
Peter Lawrey
·
Apr. 27, 12 · Interview
Like (0)
Save
Tweet
Share
8.06K Views

Join the DZone community and get the full member experience.

Join For Free
On the surface these methods do the same thing in Java; Thread.yield(), Thread.sleep(0), Object.wait(0,1) and LockSupport.parkNanos(1) They all wait a sort period of time, but how much that is varies a surprising amount and between platforms.

Timing a short delay

The following code times how long it takes to repeatedly call those methods.
import java.util.concurrent.locks.LockSupport;

public class Pausing {
    public static void main(String... args) throws InterruptedException {
        int repeat = 10000;
        for (int i = 0; i < 3; i++) {
            long time0 = System.nanoTime();
            for (int j = 0; j < repeat; j++)
                Thread.yield();
            long time1 = System.nanoTime();
            for (int j = 0; j < repeat; j++)
                Thread.sleep(0);
            long time2 = System.nanoTime();
            synchronized (Thread.class) {
                for (int j = 0; j < repeat/10; j++)
                    Thread.class.wait(0, 1);
            }
            long time3 = System.nanoTime();
            for (int j = 0; j < repeat/10; j++)
                LockSupport.parkNanos(1);
            long time4 = System.nanoTime();

            System.out.printf("The average time to yield %.1f μs, sleep(0) %.1f μs, " +
                    "wait(0,1) %.1f μs and LockSupport.parkNanos(1) %.1f μs%n",
                    (time1 - time0) / repeat / 1e3, (time2 - time1) / repeat / 1e3, 
                    (time3 - time2) / (repeat/10) / 1e3, (time4 - time3) / (repeat/10) / 1e3);
        }
    }
}

On Windows 7

The average time to yield 0.3 μs, sleep(0) 0.6 μs, wait(0,1) 999.9 μs and LockSupport.parkNanos(1) 1000.0 μs
The average time to yield 0.3 μs, sleep(0) 0.6 μs, wait(0,1) 999.5 μs and LockSupport.parkNanos(1) 1000.1 μs
The average time to yield 0.2 μs, sleep(0) 0.5 μs, wait(0,1) 1000.0 μs and LockSupport.parkNanos(1) 1000.1 μs

On RHEL 5.x

The average time to yield 1.1 μs, sleep(0) 1.1 μs, wait(0,1) 2003.8 μs and LockSupport.parkNanos(1) 3.8 μs
The average time to yield 1.1 μs, sleep(0) 1.1 μs, wait(0,1) 2004.8 μs and LockSupport.parkNanos(1) 3.4 μs
The average time to yield 1.1 μs, sleep(0) 1.1 μs, wait(0,1) 2005.6 μs and LockSupport.parkNanos(1) 3.1 μs

In summary

If you want to wait for a short period of time, you can't assume that all these methods do the same thing, nor will be the same between platforms.
Data Types Java (programming language) Sort (Unix)

Published at DZone with permission of Peter Lawrey, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Tracking Software Architecture Decisions
  • Multi-Cloud Integration
  • How To Choose the Right Streaming Database
  • 5 Software Developer Competencies: How To Recognize a Good Programmer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: