java - synchronize read of variable -
I do not understand how to do this. I have this barrier:
- A variable can be read in maximum 5 threads, others should wait in until the variable can be read again .
public zero method A () {lockI.lock (); Modify {countIreaders == 5} while trying {conditionI.ait (); } CountIreaders ++; } Grip (Interrupted e) e.printStackTrace (); } Finally {if (condition) {countIreaders = 0; LockI.unlock (); ConditionI.notifyAll (); } And {lockI.unlock (); }}}
With this code I have made it serial, but it is not what I want to achieve. How do I modify the code?
I do not really understand your code, but it seems that you have a search for one. A semaphore is useful for thread synchronization. I can make a new semaphore with a 5 token / permit in this way:
semaphore cm = new semaphore (5); // Preliminary // in your data structure ... Public Zero Your Thread Function () [//] ... in the thread of your readers: // Each thread will definitely use the same semaphore / a thread Token from semaphore before reaching its variable sem.aquire (); // This call is hanging until a permit is available. Read your value and calculate some, only 5 threads can be within this part because Aquir sem.release (); // Token / Issue Permit}
Comments
Post a Comment