java - Threads affected by Thread.yield()? -
here 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 new thread"); } }; mythread.start(); thread.yield(); system.out.println("hello main thread"); mythread.join(); } }
as understand, after mythread.start()
, there 2 threads running. 1 main thread, , other newly-created mythread
. then, thread referred in thread.yield()
?
i checked java se6 doc, says
thread.yield(): causes executing thread object temporarily pause , allow other threads execute
but in codes, can't see currently excuting thread
is, looks both threads running @ same time.
isn't more clear mythread.yield()
instead of thread.yield()
? have ideas this?
with "current thread" in context, javadoc means "the thread called method thread.yield()
"
in case, main thread started application.
as javadoc explains, there no need call thread.yield()
. it's not required anything:
a hint scheduler current thread willing yield current use of processor. scheduler free ignore hint.
it seem something, @ least java 6 - couldn't find reference java 7/8.
windows:
the hotspot vm implements
thread.yield()
using windowsswitchtothread()
api call. call makes current thread give current timeslice, not entire quantum.
linux:
under linux, hotspot calls
sched_yield()
. consequences of call little different, , possibly more severe under windows.
source: http://www.javamex.com/tutorials/threads/yield.shtml