Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8264029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:18:46+00:00 2026-06-08T04:18:46+00:00

The method Thread.yield : Causes the currently executing thread object to temporarily pause and

  • 0

The method Thread.yield:

Causes the currently executing thread object to temporarily pause and
allow other threads to execute.

So in the following code:

public class Test implements Runnable {  

    private int stopValue;  

    public Fib(int stopValue){  
        this.stopValue = stopValue;  
    }  

    @Override  
    public void run() {  

        System.out.println("In test thread");  
        for(int i = 0; i < stopValue; i++){  
            c = i + 1;  
        }  

        System.out.println("Result = "+c);        
    }  

    public static void main(String[] args){  
        int defaultStop = 1024;  
        if(args.length > 0){   
            defaultStop = Integer.parseInt(args[0]);  
        }  
        Thread a = new Thread(new Fib(defaultStop));  
        System.out.println("In main");  
        a.setDaemon(true);  
        a.start();  
        Thread.yield();       
        System.out.println("Back in main");  
    }  

}

I expect that I should see:

  1. In main then
  2. In test thread

and the rest would be undefined. But I don’t understand why sometimes I only see:
In main and Back in main and not any print statement from Test thread?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:18:47+00:00Added an answer on June 8, 2026 at 4:18 am

    yield() is a hint to the OS scheduled but doesn’t provide any guarantees in terms of scheduling. It doesn’t always pause very long. If you call it repeatly it might only take a few micro-seconds.

    Starting a thread takes time and even if you main thread pauses briefly, it may finish before the background thread starts.


    As you can see, yield() pauses very briefly.

    long start = System.nanoTime();
    long runs = 20000000;
    for (int i = 0; i < runs; i++)
        Thread.yield();
    long time = System.nanoTime() - start;
    System.out.printf("Thread.yield() took an average of %,d ns.%n", time / runs);
    

    prints

    Thread.yield() took an average of 148 ns.
    

    by comparison, System.nanoTime take longer on my machine.

    long start = System.nanoTime();
    long runs = 20000000;
    for (int i = 0; i < runs; i++)
        System.nanoTime();
    long time = System.nanoTime() - start;
    System.out.printf("System.nanoTime() took an average of %,d ns.%n", time / runs);
    

    prints

    System.nanoTime() took an average of 656 ns.
    

    Both times will vary from OS to OS and machine to machine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Thread.yield() static method (in Java) causes the currently executing thread object to temporarily
In javadoc there is said that yield method Causes the currently executing thread object
Would be thread-safe to use the yield operator inside an extension method? For example:
I am a little bit confused about the use of Thread.yield() method in Java,
Some questions regarding the Thread.yield() method. What I have understood is When we call,Thread.Yield(),
Are there overall rules/guidelines for what makes a method thread-safe? I understand that there
What is the use of Thread.BeginThreadAffinity() method in Thread class ? It will be
I am using the thread.Abort method to kill the thread, but it not working.
sleep() is a static method of class Thread. How does it work when called
If i want to override the Thread#start() method can I? and if yes will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.