How does the method work?

Javadoc says yield method

Causes a temporarily executed thread object to temporarily suspend and allow other threads to execute.

And the book by Katherine Sierra and Burt Bates SCJP states that

yield () should do this; make the current current thread head back to runnable to allow other threads with the same priority to get the queue.

So what does the method actually do?

+7
java yield multithreading
source share
7 answers

Given a multi-threaded application, yield will cause the current executable thread to pause execution and be set to standby. Then, the JVM will start starting another thread that was previously pending.

I believe that the same stream that has just lost may be officially planned for a start.

And I have yet to see it in the wild. Therefore, I think it is safe.

Develop:

In a multi-threaded environment, threads are scheduled and unplanned and run in the JVM. Thus, even if the income is not called in the code, your thread may / will automatically be inferior to other threads when the JVM decides that it is necessary. This allows you to work with multiple threads in a single core processing environment.

The calling income simply tells the JVM to put the current thread on hold, even if the JVM is not going to.

I will try to illustrate:
The following is a very simplified illustration of the execution of 2 threads over time (suppose 1 core) -

 Thread\Time 1 2 3 4 5 6 7 8 9 Thread 1 ----------- ----- ------- Thread 2 ------- ---------- ------ 

Whenever you see a '-' , this means that the thread is executing. A ' ' means the thread is waiting. As you can see, only 1 thread can actually start at a time. So, while 1 is working, another is waiting. What needs to be done is to give other threads the opportunity to run ahead of the current current thread.

+11
source share

Themes can be runnable, locked (for example, waiting for some io to finish), or started; this is common to all thread implementations, although some specific implementations may have more states.

Profitability leads to the fact that the thread changes from working to runnable and waits for the scheduler to change it to start again in the future. This is what is in the SCJP book.

To the stream, it looks like it is temporarily suspended, as described in javadoc. Thus, both statements are correct, they are simply formulated differently.

+2
source share

yield() usually used when you expect a thread for something, but don't want to block CPC loops with something like while(condition){ ...} . The way yield () works differs from platform to platform and depends on the Stream Scheduler, and you should not rely on it to behave in a certain way.

+2
source share

This comes from collaborative multitasking. The basic idea is that the processor only executes one thread so far:

  • end of this line
  • this thread performs some blocking operation, for example, object.wait() or Thread.sleep , waiting for the completion of some I / O operation, waiting for some object monitor or similar.
  • this thread calls Thread.yield() .

In each of these cases, the thread scheduler then selects a different thread to execute. Therefore, to be fair for other threads, you would call yield() regularly in longer loops without any locking operations. (If no other thread is ready to start, then the same thread will be scheduled again, so there is no big performance loss.)

In modern virtual machines, thread switching can occur at any point, and not only these listed flows can be performed at the same time, so this is really not necessary, and some virtual machines can ignore it at all (similar to System.gc() .)

+2
source share

The yield () method should ensure that all the same priority streams in the application will not call starvation . E.g. There are five threads in the application, and they all have the same priority. Now suppose that one thread got a chance to start, and this thread takes so long to complete its task, and, therefore, other threads will not have a chance to start. Therefore, to avoid such situations, yield () must save.

+2
source share

Ultimately, calling yield() invoke os methods like this, which will basically return the task to the execution queue and allow the following task ( source ) to be executed:

  /** * sys_sched_yield - yield the current processor to other threads. * * This function yields the current CPU to other tasks. If there are no * other threads running on this CPU then this function will return. */ SYSCALL_DEFINE0(sched_yield) { /* * lock this runqueue and disable interrupts. */ struct rq *rq = this_rq_lock(); schedstat_inc(rq, yld_count); current->sched_class->yield_task(rq); /* * Since we are going to call schedule() anyway, there's * no need to preempt or enable interrupts: */ __release(rq->lock); spin_release(&rq->lock.dep_map, 1, _THIS_IP_); _raw_spin_unlock(&rq->lock); preempt_enable_no_resched(); schedule(); return 0; } 
+2
source share

Hope it helps!

 package yield; public class ThreadYieldApp { Thread th1 = new Thread("Thread 1") { public void run() { for(int i = 0; i <= 10; i++) { System.out.println("Before Yield - " + Thread.currentThread().getName() + " at index - " + i); //Currently pauses the thread and checks for other threads of same priority, and passes control to it based on Thread Scheduler pick Thread.yield(); System.out.println("Currently running - " + Thread.currentThread().getName() + " at index - " + i); } } }; Thread th2 = new Thread("Thread 2") { public void run() { for(int i = 0; i <= 10; i++) { System.out.println("Currently running - " + Thread.currentThread().getName() + " at index - " + i); } } }; public static void main(String[] args) { ThreadYieldApp threadYieldApp = new ThreadYieldApp(); threadYieldApp.th1.start(); threadYieldApp.th2.start(); } //Happy coding -- Parthasarathy S } 
0
source share

All Articles