The difference between wait () and yield ()

So far, what I understood about the wait () and yield () methods is that yield () is called when a thread does not perform a task and allows the processor to execute another thread. wait () is used when some thread is put on hold and is usually used in the synchronization concept. However, I do not understand the difference in their functionality, and I'm not sure if I understood correctly what I understood. Can someone explain the difference between them (except for the package in which they are present).

+6
source share
3 answers

Don't they perform the same task - expecting other threads to be able to execute?

It does not close because yield() does not expect anything.

Each thread can be in one of several different states: Running means that the thread is actually running on the processor, Runnable means that nothing prevents the thread from starting, except, perhaps, having a processor to run it on. All other states can be combined into a category called blocked. A blocked thread is a thread that is waiting for something that can happen before it can become operational.

The operating system launches threads regularly: every time (from 10 times per second to 100 times per second on most operating systems) the OS loses each thread and says: "Your turn is up, go to (i.e. change the state from start to runnable) Then it allows any thread at the head of the execution queue to use this CPU (ie, start it again).

When your program calls Thread.yield() , it tells the operating system: “I still have work, but it may not be as important as the work that some other thread does. Please send me to the back of the line launch right now. " If there is an available CPU for the thread to be executed, then it will continue to work efficiently (i.e., the call to yield () will return immediately).

When your program calls foobar.wait() , on the other hand, it tells the operating system: "Block me until some other thread calls foobar.notify() .

Disposal was first implemented on unmanaged operating systems and in uncaught thread libraries. On a computer with one processor, the only way that ever had to run more than one thread was that the threads were clearly inferior to each other.

Accounting is also useful for lively waiting. That the stream expects something to happen, sitting in a tight loop, testing the same condition again and again. If the condition depended on some other thread to do some work, the waiting thread will give () each time around the loop to let the other thread do its work.

Now that we have security and multiprocessing systems and libraries that provide us with higher-level synchronization objects, there are basically no reasons why applications need to call yield() .

+16
source

wait intended to wait on condition. This may not be striking when looking at a method, as it is completely up to you to determine what state it is. But the API is trying to get you to use it correctly by requiring that you have a monitor of the object on which you expect what is needed to properly check the state in a multi-threaded environment.

Therefore, the correct use of wait as follows:

 synchronized(object) { while( ! /* your defined condition */) object.wait(); /* execute other critical actions if needed */ } 

And it must be paired with another thread executing the code, for example:

 synchronized(object) { /* make your defined condition true */) object.notify(); } 

On the contrary, Thread.yield() is just a hint that your thread may release the CPU at this point in time. It is not indicated whether it really does anything, and regardless of whether the processor was released or not, it does not affect the semantics regarding the memory model. In other words, it does not create any connection with other threads that are necessary for proper access to shared variables.

For example, the following loop, accessing sharedVariable (which is not declared volatile ), can run forever without even noticing updates made by other threads:

 while(sharedVariable != expectedValue) Thread.yield(); 

Although Thread.yield can help other threads to start (they will work anyway on most systems), it does not provide a read- sharedVariable of the sharedVariable value from shared memory. Thus, without other designs that provide memory visibility, for example. decaring sharedVariable as volatile , this loop does not work.

+7
source

the first difference is that yield() is a Thread method, wait() is at the beginning of the Object path of the inheritid method in Thread as for all classes that are in the form, in the background (using java doc)

 wait() 

Causes the current thread to wait until another thread calls the notify () method or the notifyAll () method for this object. In other words, this method behaves exactly as if it was just waiting for a call (0).

 yield() 

A hint to the scheduler that the current thread is ready to provide the current processor usage. The scheduler may ignore this hint.

and here you can see the difference between yield () and wait ()

+3
source

Source: https://habr.com/ru/post/977871/


All Articles