java - Giving fairness in execution for threads -


consider situation thread has list of strings {string1,string2} , thread b has {string3, string4}. both threads trying execute block of code, lets

  public void critcalcodemethod(){     ....     ....     } 

when thread , b in action, suppose thread c has {string1,string5} , thread d has {string3, string 6} trying access same critcalcodemethod() code, should put in fairness queue , should allowed execute in order in arrived. in other words, thread , b can execute in parallel since dont have common strings. thread c should executed first right after thread finishes. thread d should executed first right after thread b. if other threads thread e value {string1, string7} should allowed after thread c. please suggest locking mechanism achieve in java

reentrantlock has optional fairness parameter can used implement logic.

however, depending on strings are, come , how you're using them there may more suitable ways handle concurrency (guava's striped comes mind).


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -