Machine-translated by Google. Questions are authored in English.
Top

Discussion

Sample65 is run - what will be the output?

public class Sample65
{
    static final Runnable POLITE = () ->
    {
        for (int spin = 0; spin < 3; spin++)
        {
            Thread.yield();
        }
        System.out.print("worker");
    };

    public static void main(String[] args) throws InterruptedException
    {
        Thread courteous = new Thread(POLITE);
        courteous.start();
        courteous.join();
        System.out.print(" main");
    }
}

  • A.worker main
  • B.main worker
  • C.Either order
  • D.worker only

Answer: A

yield is only a hint that the scheduler may ignore, and it never ends a thread or transfers control to a named one, however many times it is called. Whatever it does or does not do, the join still guarantees the worker finishes before main's print, so the order is fixed by the join alone.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.