[英]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数据包。 two
in run_loop
method. two
中run_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.