[英]Execution on synchronized and non-synchronized method in the same class
我在一个类中有两种方法,一种是同步的,另一种是非同步的。 当我用两个不同的线程调用这些方法时,我看到执行变成串行而不是并行的。
我很困惑,根据理论, r.processSomething()不应该等待r.doSomething()完成其执行。
如果有人对同时执行两种方法(同步和非同步)有所了解。 请分享。
class LockObjectDemo{
public static void main(String[] args){
SharedResource r = new SharedResource();
MyThread t1 = new MyThread(r);
t1.start();
MyThread t2 = new MyThread(r);
t2.start();
}
}
class MyThread extends Thread{
private SharedResource r ;
MyThread(SharedResource r){
this.r = r;
}
public void run() {
try {
r.doSomething(); // Only after completing this method , the other thread enters into the next method which is not locked
r.processSomething();
} catch(Exception ex){
System.out.println(ex);
}
}
}
class SharedResource{
public void doSomething() throws Exception{
synchronized(this){
System.out.println("Doing Something -> "+Thread.currentThread().getName());
Thread.sleep(5000);
System.out.println("Done Something->"+Thread.currentThread().getName());
}
}
public void processSomething() throws Exception{
System.out.println("Processing Something -> "+Thread.currentThread().getName());
Thread.sleep(1000);
System.out.println("Done Processing->"+Thread.currentThread().getName());
}
}
您有2件事,大约要花5秒钟的时间来串行执行(由于synchronized
)。
当第一个线程完成5s动作时,它开始花费约1s的时间,同时,第二个线程开始5s动作。
在第二个线程完成5s动作之前,第一个线程将完成1s动作。 然后,第二个线程执行1s动作。
因此,1s动作不会同时执行。
图形化:
Thread 1: |<----- 5s ----->|<- 1s ->|
Thread 2: |<----- 5s ----->|<- 1s ->|
对“ @Andy Turner”共享的上述解决方案的进一步分析。
在此问题中,我们必须考虑以下两点。
1)MyThread-t1和t2共享相同的SharedResource r。 这将限制“ doSomething”的执行,因此执行将以串行方式发生。 根据首先选择哪个线程,另一个线程必须等到第一个线程执行成功完成。 即睡眠5秒。
对于这个问题,让我们假设
a)线程t1首先开始运行,即t1.doSomething()
b)由于t2依赖于同步锁,并且t1在开始“ processSomething”之前正在等待第一个方法“ doSomething”完成,因此其他线程(即t1和t2)都无法执行任何其他方法。
2)一旦第一个线程(t1)“ doSomething”成功执行,两个线程就可以同时执行。
这里的线程将按照上面的“ @Andy Turner”的注释执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.