简体   繁体   English

使用同一实例从另一个线程访问同步方法

[英]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. 我在我的项目中有一个核心方法,我需要对其进行synchronized ,以使其不被同时访问两次,因此我有一个线程,该线程使用此类中的实例来访问此方法,但在该线程中我需要使用一个长寿命的循环来访问具有固定值的相同方法,因此我必须使用另一个线程才能允许第一个线程继续运行并完成其职责,但是可以肯定的是,该方法不会从第二个线程使用第一个线程中使用的相同实例从第二个线程运行,并且由于某种原因我无法实例化该类中的另一个实例,因为我必须确切地使用该实例,因此如何克服此问题。

below is the problem translated to java: 下面是将问题翻译为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. my_method --->是发送UDP数据包。
  • 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. 在某些时候,我必须重复地发送保持有效消息的每个10秒,所以,我必须启动该单独的线程是线程tworun_loop方法。

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; 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();
   }
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程调用同步方法是否使用相同的对象抢占另一个线程但是以非同步的方式? - Does a thread invoking, synchronized method, pre-empt another thread using same object but in a non synchronized manner? 从Java中同一类的另一个同步方法内部创建的新线程中调用同步方法 - Calling a synchronized method from a new thread created inside another synchronized method of the same class in Java 我们可以同时从多个线程访问同一个实例的同步方法和非同步方法吗? - Can we Access Synchronized method and an unsynchronized method of same instance from multiple threads at the same time? 为什么此线程允许另一个线程访问其同步方法? - Why is this thread allowing another one to access its synchronized method? 在同一实例上使用同步方法锁定 - Locking with synchronized method on same instance 同步方法在停止一个线程与另一个线程之间的重要性是什么? - What is the importance of synchronized method in stopping one Thread from another? 在多线程环境中从另一个同步方法同步的影响? - Impact of one synchronized from another synchronized method in multi-thread environment? 使用synchronized但从结果IllegalMonitorStateException通知来自另一个类的线程 - Notify Thread from another class using synchronized but result IllegalMonitorStateException 更多线程可以同步同一实例的方法 - More threads to synchronized method of same instance 如果从同步方法中调用方法,则内部方法对共享的可变数据的访问线程安全吗? - If a method is called from within a synchronized method, is the inner method's access to shared mutable data thread-safe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM