multithreading - What is fast, wait notify or busy wait in Java? -


I think the use of busy-waiting is not a good programming practice and preferably the use of synchronized object (wait-awakened) Should be done whenever possible. But I would like to know if someone is ready to sacrifice the CPU chakras, then what will be busy or wait-to inform?

I think the waiting-informed synchronized object will include an internal lock and it can also come to wake the thread from the signal kernel, this approach is much more than a busy-wait Slow down, where one just checks a condition until it's satisfied. As soon as this situation is satisfied (eg Boolean value == true), the thread can come out of busy waiting. According to my understanding, I think busy-waiting should be fast.

I appreciate if others can share their thoughts and correct me if my argument is wrong.

Experiments show that if you wait and wait for your expenditures to notify , Then you will see the flag soon (on my hardware, anyway). (Description below.) The difference is very strong too and therefore it will only apply to very rare apps. For example, stock trading applications, where companies can get after any profit (to improve Microsoft's exchange from the Exchange as much as possible, to find their servers near the exchange and feeds from such exchange is). I can also imagine some science applications.

In most apps, the difference effect will not make any difference.

But what happens to the CPU is, of course, one of the core hard-pegs:

 hardpeg

It is very bad in the context of affecting other processes and consumption of power in the data center.

Use this: With extreme inconvenience, only in those situations where it really matters.


Data (very small sample, but the code is below):

 Wait for the wait: 10631 12350 15278 Wait and Notification: 87299 120 9 64 107204 Delta: 76668 108614 91926  

The Times Nano are the second billion billionaire delta 92403ns (0.092402667 milliseconds, 0.000092403 sec) in seconds.

BusyWait.java :

  public class BusyWait {Private Static Share Share {public long time set et; Public seen long; Public volatile boolean flag = false; } Public static zero main (string [] args) {last shared share = new share (); Thread Notifier = New Thread (New Runnable () {Public Zero Run}} {System.out.println ("Running"); Try {Thread, sleep (500); System.out.println ("Setting Flag "); Shared .setAt = System.nanoTime (); shared.flag = true;} Catch (Exception E) {}}}); Notifier.start (); While (! Shared.flag) {} shared.seenAt = System.nanoTime (); System.out.println ("Delay between set and seen:" + (shared .seenAt - shared.setAt)); }}   

WaitAndNotify.java :

  public class WaitAndNotify {share private stationary class {public long time set; Public seen long; Public boolean flag = false; } Public static zero main (string [] args) {(New WaitAndNotify ()). testing (); } Private Zero Test () {Last Shared Share = New Shared (); Last wait and note the event = this; Thread Notifier = New Thread (New Runnable () {Public Zero Run}} {System.out.println ("Running"); Try {Thread, sleep (500); System.out.println ("Setting Flag "); Shared .setAt = System.nanoTime (); shared.flag = true; synchronize (example) {instance.notify ();}} hold (exception e) {}}}); Notifier.start (); While (! Shared.flag) {try {synchronize (this) {wait (); }} Grip (ie interrupted ejection) {}} shared.seenAt = System.nanoTime (); System.out.println ("Delay between set and seen:" + (shared .seenAt - shared.setAt)); }}    

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -