multithreading - Using volatile to ensure visibility of shared (but not concurrent) data in Java -
I am trying to implement a fast version of LZ77 and I want you to ask about concurrent programming Have a question. For now, I have a The main thread writes all the buffers, then indicates the thread and wait for them to complete. The single task thread processes a portion of the buffer saving the results in the same part of the result holder. Part of the worker is unique, after which the main The thread gets notified and the worker pauses. When all workers are stopped, the head thread reads the data in the header and buffer updates, then (if necessary) the process restarts from 1 point . Important things in the manager (main thread) are declared as the following: The MemoryHelper simply wraps an unstable area and offers two ways: one to read it and to write it. worker's run () code: Synchronization works well on ArrayBlockingQueue, I suspect that using So this is enough in this case to ensure the correct visibility? There is no uniform concurrent access to the same memory areas, so a volatile field should be much cheaper than ReadWriteLock. You do not even need a Memory Stability Impact: Along with other concurrent collections, actions to remove or remove an element in another element from the In general, if you already have some sort of synchronization, then you probably do not have to do anything special to ensure memory visibility because it has already been used The synchronization going is guaranteed by premitive. However, P. s. Also it seems that you can use queue instead of your solution, it is specially designed for similar scenarios. last byte [] buffer and a
final integer [] result holder , both of which do the same length program: / p>
last byte [] buffer = new byte [SIZE] ; Last MemoryHelper MemoryHelper = new memory helper (); Last Arrawlocking Quiz & lt; Object & gt; Waiting buffer = new arrawlocking quiz & lt; Object & gt; (TOT_WORKERS); Last Arrawlocking Quiz & lt; Object & gt; Wait = new ArrayBlockingQueue & lt; Object & gt; (TOT_WORKERS); Final integer [] resulthilder = new int [size];
public void run (try {main} {try} {manager.waitBuffer.take (=!) = SHUTDOWN ) {// Load new Buffer Price Manager. Memory. Helper Reed Villatile (); // For some (int i = a; i & lt; = b; i ++) {manager.resultHolder [i] = manager.buffer [i] + 10; } // Flush the new values of the resultsholler manager.mobilehalers.writewalltail (); // Signal job was completed manager. Result.ed (Object Class); Finally, the important part of the main thread:
for
(int i = 0; i & lt; 100_000; i ++) {// Get started for workers (int j = 0 ; J & LT; TOT_WORKERS; J ++) wait buffer.ed (Object.class); // Wait for the workers (int j = 0; j & lt; toT_WORKERS; j ++) Wait for the cent. // Load Results Memory Helper Reed Villatile (); // Results of some process (); SetBuffer (); // store buffer memoryhaller Written volatile (); }
readVolatile () and
written voltile () Used to be. I have been told that writing a volatile area flashes memory all previously modified data , then it reads it from another thread and shows them.
unstable here, because already required Memory Visibility Guarantee:
blocking question In a thread, before placing an object in a
blocking function.
vapor can be used to ensure memory visibility (eg, in the lock-free algorithm when reading and writing, when you do not have a clear synchronization ).
Comments
Post a Comment