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 7668237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:18:43+00:00 2026-05-31T15:18:43+00:00

Some questions regarding the Thread.yield() method. What I have understood is When we call,Thread.Yield(),

  • 0

Some questions regarding the Thread.yield() method. What I have understood is When we call,Thread.Yield(), the currently running thread will go back to the runnable state.So dependes on the thread priority thread scheduler will execute next higher priority thread. Now I have one sample program. Please see below.

package thread;

public class YieldTest implements Runnable {

    @Override
    public synchronized  void run() {
        System.out.println(Thread.currentThread().getName()+", executing..");
        for(int i = 0 ; i <5;i++){
            if(i==2){               
                Thread.yield();
                System.out.println(Thread.currentThread().getName()+": "+i+" yielded()");
                /*try {
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread().getName()+": "+i+" yielded()");
                } catch (InterruptedException e) {

                    e.printStackTrace();
                }*/

            }else{
                System.out.println(Thread.currentThread().getName()+": "+i);
            }

        }       
    }


public static void main(String[] args) {

    YieldTest test = new YieldTest();
        Thread t1 = new Thread(test);
        t1.setName("A");
        t1.setPriority(9);
        Thread t2 = new Thread(test);
        t2.setName("B");
        Thread t3 = new Thread(test);
        t3.setName("C");

        t2.setPriority(6);
        t2.setPriority(4);

        t1.start();
        t2.start();
        t3.start();

    }


}

Here I am getting the output always in the following way,

A, executing..
A: 0
A: 1
A: 2 yielded()
A: 3
A: 4
C, executing..
C: 0
C: 1
C: 2 yielded()
C: 3
C: 4
B, executing..
B: 0
B: 1
B: 2 yielded()
B: 3
B: 4

Now the question is yield() method should go back to the runable state and other thread should execute. so the output should be something like the following way

A, executing..
A: 0
A: 1
A: 2 yielded()

C, executing..
C: 0
C: 1
C: 2 yielded()

A: 3
A: 4

B, executing..
B: 0
B: 1
B: 2 yielded()

C: 3
C: 4

B: 3
B: 4

Also, what about the thread priority. Is it not guaranteed why do we need thread priority?Please correct me If I am wrong.

  • 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-05-31T15:18:44+00:00Added an answer on May 31, 2026 at 3:18 pm

    All your run methods are synchronized on the same object (test). So yielding doesnt acheive anything, since until the first thread’s run method exits, no others can enter their run() methods.

    Basically thread 1 starts, and enters the run() method. This is synchronized on ‘this’. After outputting 2 numbers, it yields. But threads 2 and 3 are both waiting to enter the run method, since they cannot obtain a monitor on test. So yield doesnt do anything. The same issue then happens once thread one has finished.

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

Sidebar

Related Questions

I have couple questions regarding some C++ rules. Why am I able to call
I've read some documentation on how Adaboost works but have some questions regarding it.
There have been some similar questions asked regarding Grid views, but none have been
Hi I have some questions regarding Looper.prepare() and AsyncTasks. In my app I have
I've got a question regarding multiple-thread method invocation in Java. Let's say we have
I have some questions regarding threads: What is the maximum number of threads allowed
I have some questions regarding read-write locks in POSIX Pthreads on a *nix system,
I had some questions regarding the structure and behavior of a model, using UML,
I've got some questions regarding Java design patterns. I want to know what the
I have a question regarding the some data which is being transfered from one

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.