0

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.
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
  • In your description, refer to the actual methods you provided in the code, it will be easier for anyone willing to help – Luigi Cortese Mar 06 '16 at 14:49
  • "// won't run" don't you mean won't compile?? AFAIK, there's no class_one defined here –  Mar 06 '16 at 14:50
  • 1
    it won't compile because of an unhandled exception in `Thread.sleep`. OP, I'm afraid you didn't provide a meaningful example in your code – Luigi Cortese Mar 06 '16 at 14:51
  • There's no reason for the call in thread two to block, since thread one's call to the method has returned before thread two is even started. Fixing the compilation error and running the posted code shows that my_method is called repeatedly. – JB Nizet Mar 06 '16 at 14:58
  • @LuigiCortese the actual methods will make the code vague to others, I did my best to make the idea clear by this code. And don't pay attenstion to the unhandled exceptions as all the exceptions are handled in the main code. You can also have a look at the additions to the question which explains the problem overview. – Muhammed Refaat Mar 08 '16 at 09:59
  • @RC. there is an instance of `ClassOne` passed to the method `run_loop`. You can also have a look at the additions to the question which explains the problem overview. – Muhammed Refaat Mar 08 '16 at 10:00
  • @JBNizet That's what I'm wanting to achieve(calling my_method in a separate thread so that it won't prevent the first thread from continuing it's work). You can also have a look at the additions to the question which explains the problem overview. – Muhammed Refaat Mar 08 '16 at 10:01

1 Answers1

0

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 Roy
  • 2,178
  • 1
  • 17
  • 33