ok, reordering is not a good idea to consider here. But, please note that 
if conifgureBlocking wans't synchronized then a statement:

blocking = false 

could be "skipped" on compilation level because JMM doesn't guarantee you 
that every access to the memory will be "commit" to the main memory. 


W dniu piątek, 9 marca 2018 23:20:37 UTC+1 użytkownik John Hening napisał:
>
>
>     executor = Executors.newFixedThreadPool(16);
>     while(true) {
>         SocketChannel connection = serverSocketChannel.accept();
>         connection.configueBlocking(false);
>         executor.execute(() -> writeTask(connection)); 
>     }
>     void writeTask(SocketChannel s){
>         s.isBlocking();
>     }
>
>     public final SelectableChannel configureBlocking(boolean block) throws 
> IOException
>     {
>         synchronized (regLock) {
>             ...
>             blocking = block;
>         }
>         return this;
>     }
>
>
>
> We see the following situation: the main thread is setting 
> connection.configueBlocking(false)
>
> and another thread (launched by executor) is reading that. So, it looks 
> like a datarace.
>
> My question is:
>
> 1. Here 
> configureBlocking
>
> is synchronized so it behaves as memory barrier. It means that code is ok- 
> even if reading/writing to 
> blocking
>
> field is not synchronized- reading/writing boolean is atomic.
>
> 2. What if 
> configureBlocking
>
> wouldn't be synchronized? What in a such situation? I think that it would 
> be necessary to emit a memory barrier because it is theoretically possible 
> that setting blocking field could be reordered. 
>
> Am I right?
>

-- 
You received this message because you are subscribed to the Google Groups 
"mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to