2

Here is a multi-threaded HelloWorld:

public class HelloWorld {

    public static void main(String[] args) throws InterruptedException {
        Thread myThread = new Thread() {
            public void run() {
                System.out.println("Hello World from new thread");
            }
        };
        myThread.start();
        Thread.yield();
        System.out.println("Hello from main thread");
        myThread.join();
    }
}

As I understand, after the myThread.start(), there will be two threads running. One is the main thread, and the other is the newly-created myThread. Then, which thread is referred to in the Thread.yield()?

I checked the Java SE6 Doc, which says

Thread.yield(): Causes the currently executing thread object to temporarily pause and allow other threads to execute

But in the codes, I can't see clearly what the currently excuting thread is, it looks that both threads are running at the same time.

Isn't it be more clear to say myThread.yield() instead of Thread.yield()? Does anyone have ideas about this?

mins
  • 6,478
  • 12
  • 56
  • 75
Hanfei Sun
  • 45,281
  • 39
  • 129
  • 237

3 Answers3

5

With "current thread" in this context, the Javadoc means "the thread that called the method Thread.yield()"

In your case, this is the main thread that started your application.

As the Javadoc explains, there is normally no need to call Thread.yield(). It's not required to do anything:

A hint to the scheduler that the current thread is willing to yield its current use of a processor. The scheduler is free to ignore this hint.

It does seem to do something, at least up to Java 6 - couldn't find a reference for Java 7/8.

Windows:

The Hotspot VM now implements Thread.yield() using the Windows SwitchToThread() API call. This call makes the current thread give up its current timeslice, but not its entire quantum.

Linux:

Under Linux, Hotspot simply calls sched_yield(). The consequences of this call are a little different, and possibly more severe than under Windows.

Source: http://www.javamex.com/tutorials/threads/yield.shtml

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • Not just this contex. When "current thread" appears in the documentation of almost _any_ method it means "the thread that calls the method". – Solomon Slow Apr 23 '15 at 13:22
4

The current thread is affected, i.e. the thread that calls the method.

Isn't it be more clear to say myThread.yield() instead of Thread.yield()? Does anyone have ideas about this?

No it isn't. It's a static method, and it's well-specified. In this case it would also convey the opposite of what actually happens.

But it's also pointless. yield() functions haven't done anything useful since the 16-bit part of Windows 98. Your thread will get rescheduled anyway as the scheduler sees fit.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

The main thread will be affected. You always can do Thread.currentThread().getName() to get the current thread name. also you can do

 Thread.currentThread().yield()

the other thread is running the run method until it finished and than exit.

igreenfield
  • 1,618
  • 19
  • 36
  • 1
    It's called `currentThread()` (with a 't' and not a 'd'), and it's a function - not a field - so you should put parentheses `()` behind it to invoke the function. – Erwin Bolwidt Apr 23 '15 at 05:33