Synchronous justice

I am using 1producer-1consumer design in my application using SynchronousQueue. By now I am using it with the standard constructor (fair = true). And I'm wondering how "fair = false" will affect the system (performance and especially concurrency behavior).

Here's what the docs say:

SynchronousQueue

public SynchronousQueue ()

Creates a SynchronousQueue with nonfair access policy.

      

SynchronousQueue

public SynchronousQueue (boolean fair)

Creates a SynchronousQueue with the specified fairness policy.

Parameters:
    fair - if true, waiting threads contend in FIFO order for

      

access; otherwise, the order is undefined.

Thanks in advance.

+2


source to share


2 answers


Your question contains the answer, more or less. Anyway, the short answer is that it won't effectively affect your single consumer case (with perhaps a slight performance degradation).

If you set the flag fair

to true, then, as you inserted in your question, waiting streams in FIFO order to access. This imposes certain restrictions on the scheduling of waiting threads as to how they wake up; an unfair system does not have such restrictions (and hence the compiler / runtime can do things that might be slightly faster).



Note that it only ever fires which thread is selected to wake up from the set of waiting threads; and with only one thread that will ever wait, the decision algorithm is irrelevant as it will always select the same thread. The difference happens when you have multiple threads waiting - is it acceptable for one individual thread to never get anything from the queue if other threads can handle all the workload in between?

+3


source


Wrt. have you tried measuring this? This will likely give you more information on what's going on than any answer here.

From the doc :



Equity usually decreases bandwidth, but reduces variability and avoids starvation

but it would be interesting to run a repeatable test and see how this will affect you and your particular circumstances. Since you only have one consumer thread, I don't think this will affect your application beyond the (perhaps) slight (perhaps imperceptible?) Performance degradation. But I would repeat that you should try to measure it.

+1


source







All Articles