multithreading - Syncronisize two looping Java-Thread -


two threads running parallel:

thread1:

while(...) {   <-- wait until thread2 not in update()   dowork(); } 

thread2:

while(...) {   dowork();   <-- wait until thread1 not in work()   update(); } 

i think examples above explain try do, have not idea how synchronization. update()-method of thread2 critical while executed thread2 have wait.

edit:

thanks far answers. more 1 working well. asked trying , want give short update that.

based on currentstate thread2 calculates nextstate , swap both before repeats calculating endless. thread1 displays 'currentstate' in gui user.

thread1 should not display currentstate while swaping in progress.

thats all.

one way use locks (look @ java.util.concurrent.locks package) i'd first think whether approach in general improved, e.g. lock-free algorithms etc.

another way use of synchronized methods/blocks on shared object. depends on you're trying achieve, however.

example using synchronized block:

t1:

while(...) {   synchronized( someclass.class ) {     dowork();   } } 

t2:

while(...) {   dowork();   synchronized( someclass.class ) {     update();   } } 

here you'd synchronize on same instance of class<someclass> work long you're using same classloader.

keep in mind should make synchronized blocks small possible in order not add unnecessary blocking.

besides that, note synchronized block in t1 might make hard t2 break in between 2 iterations. question, however, why designed way.

edit

as alternative synchronizing entire dowork() call might want think needs synchronized.

as example, take following pseudo code:

  workresult dowork(sharedobject so) {     data data = so.loaddata();     workresult wr = dosomelengthywork(data);     return wr;   }    void update(workresult wr, sharedobject so) {     so.updatefromworkresult( wr );   } 

if situation this, synchronize calls so.loaddata() , so.updatefromworkresult() , let lengthy operation work on copy of data provided so.loaddata().

edit 2:

alternatively use readwritelock implementation:

t1:  while(...) {   lock readlock = readwritelock.readlock();   readlock.lock();    dowork();    readlock.unlock();   } 

t2:

while(...) {   dowork();    lock writelock= readwritelock.writelock();   writelock.lock();    update();    writelock.unlock(); } 

note left exception handling etc. out simplicity.

what you're doing here aquire read lock during work , write lock while updating. multiple threads can work in parallel unless there's write lock (multiple read locks allowed) , updates wait until read locks have been released. achieve use reentrantreadwritelock in fair mode should issue locks in order requested, i.e. when t2 asks lock while t1 still reading lock next, despite t1 asking lock again.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -