简体   繁体   中英

Access synchronized method from another thread using same instance

I've a core method in my project which I need it to be synchronized in order not to be accessed twice at the same time, and hence I have a thread which uses an instance from this class to access this method, but inside this thread I need to have a long life loop to be used to access the same method with a fixed value so I have to use another thread in order to allow the first thread to move on and complete it's duties, but for sure the method doesn't run from that second thread using the same instance used in the first thread, and somehow I can't instantiate another instance from the class as I have to use this instance exactly, so how to overcome this problem.

below is the problem translated to java:

public class ClassOne {
    synchronized public void my_method(int number) {
        // Do some Work
    }
}

public class ClassTwo {

    private void some_method() {
        Thread one = new Thread(new Runnable() {
            @Override
            public void run() {
                ClassOne class_one = new ClassOne();
                // DO Work
                class_one.my_method(0);
                run_loop(class_one);
                // Complete Work
            }
        });
        one.start();
    }

    boolean running = true;

    private void run_loop(final ClassOne class_one) {
        Thread two = new Thread(new Runnable() {

            @Override
            public void run() {
                while (running) {
                    class_one.my_method(1); // won't run
                    Thread.sleep(10000);
                }
            }
        });
        two.start();
    }

}

Actual problem overview:

  • my_method --- > is to send UDP packets.
  • the method has to be synchronized otherwise I'll get the socket is already open exception when trying to use it more than once repeatedly.
  • at some point, I have to send a KeepAlive message repeatedly each 10 seconds, so, I have to launch a separate thread for that which is thread two in run_loop method.

Putting something that will compile and work. I don't see why you need this function to be synchronized. Check the output for this program...The second thread access this method only when the first thread is done accessing (unless you have missed adding some additional code).

class ClassOne {    
   int criticalData = 1;
   synchronized public void my_method(int number) {
     // Do some Work
     criticalData *= 31;
     System.out.println("Critical data:" + criticalData + "[" + Thread.currentThread().getName() + "]");
   }
 }

class ClassTwo { boolean running = true;

  public void some_method() {

    Thread one = new Thread(new Runnable() {
        public void run() {
            ClassOne class_one = new ClassOne();
            // DO Work
            class_one.my_method(0);
            run_loop(class_one);
            // Complete Work
        }
    });
    one.start();
}

public void run_loop(final ClassOne class_one) {
     Thread two = new Thread(new Runnable() {

        public void run() {
            while (running) {
                class_one.my_method(1); // won't run
                 try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
             }
         }
     });
     two.start();
   }
}

 public class StackExchangeProblem {
   public static void main(String[] args) {
     ClassTwo two = new ClassTwo();
     two.some_method();
   }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM