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 last byte [] buffer and a final integer [] result holder , both of which do the same length program: / p>

  1. The main thread writes all the buffers, then indicates the thread and wait for them to complete.

  2. 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.

  3. 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:

      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];   

    The MemoryHelper simply wraps an unstable area and offers two ways: one to read it and to write it.

    worker's run () code:

      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 (); }   

    Synchronization works well on ArrayBlockingQueue, I suspect that using 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.

    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 unstable here, because already required Memory Visibility Guarantee:

    Memory Stability Impact: Along with other concurrent collections, actions to remove or remove an element in another element from the blocking question In a thread, before placing an object in a blocking function.

    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, 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 ).

    P. s.

    Also it seems that you can use queue instead of your solution, it is specially designed for similar scenarios.

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -