java - Mutually exclusive methods -


i on way learning java multithread programming. have following logic:

suppose have class a

class {     concurrentmap<k, v> map;      public void somemethod1 () {         // operation 1 on map         // operation 2 on map     }      public void somemethod2 () {         // operation 3 on map         // operation 4 on map     } } 

now don't need synchronization of operations in "somemethod1" or "somemethod2". means if there 2 threads calling "somemethod1" @ same time, don't need serialize these operations (because concurrentmap job).

but hope "somemethod1" , "somemethod2" mutex of each other, means when thread executing "somemethod1", thread should wait enter "somemethod2" (but thread should allowed enter "somemethod1").

so, in short, there way can make "somemethod1" , "somemethod2" not mutex of mutex of each other?

i hope stated question clear enough...

thanks!

i tried couple attempts higher-level constructs, nothing quite came mind. think may occasion drop down low level apis:

edit: think you're trying set problem inherently tricky (see second last paragraph) , not needed (see last paragraph). said, here's how done, , i'll leave color commentary end of answer.

private int somemethod1invocations = 0; private int somemethod2invocations = 0;  public void somemethod1() {     synchronized(this) {         // wait there no somemethod2 invocations --         // don't wait on somemethod1 invocations.         // once somemethod2s done, increment somemethod1invocations         // signify we're running, , proceed         while (somemethod2invocations > 0)             wait();         somemethod1invocations++;     }      // code here      synchronized (this) {         // we're done method, decrement somemethod1invocations         // , wake threads waiting hit 0.         somemethod1invocations--;         notifyall();     } }  public void somemethod2() {     // comments ditto above     synchronized(this) {         while (somemethod1invocations > 0)             wait();         somemethod2invocations++;     }      // code here     synchronized(this) {         somemethod2invocations--;         notifyall();     } } 

one glaring problem above can lead thread starvation. instance, somemethod1() running (and blocking somemethod2()s), , it's finish, thread comes along , invokes somemethod1(). proceeds fine, , finishes another thread starts somemethod1(), , on. in scenario, somemethod2() never chance run. that's not directly bug in above code; it's problem design needs, 1 solution should actively work solve. think fair abstractqueuedsynchronizer trick, though exercise left reader. :)

finally, can't resist interject opinion: given concurrenthashmap operations pretty darn quick, better off putting single mutex around both methods , being done it. yes, threads have queue invoke somemethod1(), each thread finish turn (and let other threads proceed) extremely quickly. shouldn't problem.


Comments

Popular posts from this blog

meteor - inserting data to database gives error "insert failed: Method '/texts/insert' not found" -

angular - DownloadURL return null in below code -