java - How to lock an instance when a method is being executed -


I do not know whether this question is useless or not, but I can find the answer after looking for 2 hours ...

I am trying to create a program that uses multiple threads and a shared instance of a class when a certain method is being called on this example, then we will call it "reconnect" Say, the other method should wait for the call until the "reconnect" is executed. It is not finished. How do I get it?

Is it just:

  synchronize (this) {////////}   

or: / P>

  Public ReentrantLock Lock = New ReentrantLock (); Public Zero Re-connect () {lock.lock (); //// lock unlock(); }   

Sorry if it was asked earlier. Thanks for reading.

Addition 1:

It was mentioned in the comments, when a method is being executed in addition to "reconnect", and is called "reconnect", "again Connect "should wait until every method is executed.

Depends on the complexity of your program. First is enough for very basic functions. The latter (locked objects) is part of high level concurrency objects suitable for more advanced functions. The biggest advantage of lock objects is the underlying locks, their ability to withdraw from attempts to get a lock.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -