我们可以同时从多个线程访问同一实例的同步方法和非同步方法吗?
Can we Access Synchronized method and an unsynchronized method of same instance from multiple threads at the same time?
这似乎是一个非常幼稚的问题,但我找不到任何具体的答案。我什至实际尝试过,但由于我们无法预测 Java 中线程资源分配的行为,因此很难确定。
我只想知道我是否可以从 class 的同一实例的两个不同线程同时访问 class 的同步方法和非同步方法?
没有发现任何问题。试试这个:
public class Main {
public static final SyncNotsynced sn = new SyncNotsynced();
public static void main(String[] args){
Thread t1 = new Thread(sn::synced);
Thread t2 = new Thread(sn::notsynced);
t1.start();
t2.start();
}
public static class SyncNotsynced {
public synchronized void synced(){
System.out.println(Thread.currentThread().getName() + " enter synced");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println(Thread.currentThread().getName() + " exit synced");
}
public void notsynced(){
System.out.println(Thread.currentThread().getName() + " enter notsynced");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println(Thread.currentThread().getName() + " exit notsynced");
}
}
}
或查看 live example :)。如您所见,Thread 1
和 Thread 2
的输入都发生在退出之前:
Thread-0 enter synced
Thread-1 enter notsynced
Thread-0 exit synced
Thread-1 exit notsynced
您可以阅读JLS 17以获得正式的解释,但简而言之,只有一个线程可以进入同一个对象监视器上的同步块。
顺便说一句,我使用了 Thread.sleep
because(强调我的):
Thread.sleep causes the currently executing thread to sleep
(temporarily cease execution) for the specified duration, subject to
the precision and accuracy of system timers and schedulers. The thread
does not lose ownership of any monitors, and resumption of execution
will depend on scheduling and the availability of processors on which
to execute the thread.
这似乎是一个非常幼稚的问题,但我找不到任何具体的答案。我什至实际尝试过,但由于我们无法预测 Java 中线程资源分配的行为,因此很难确定。 我只想知道我是否可以从 class 的同一实例的两个不同线程同时访问 class 的同步方法和非同步方法?
没有发现任何问题。试试这个:
public class Main {
public static final SyncNotsynced sn = new SyncNotsynced();
public static void main(String[] args){
Thread t1 = new Thread(sn::synced);
Thread t2 = new Thread(sn::notsynced);
t1.start();
t2.start();
}
public static class SyncNotsynced {
public synchronized void synced(){
System.out.println(Thread.currentThread().getName() + " enter synced");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println(Thread.currentThread().getName() + " exit synced");
}
public void notsynced(){
System.out.println(Thread.currentThread().getName() + " enter notsynced");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println(Thread.currentThread().getName() + " exit notsynced");
}
}
}
或查看 live example :)。如您所见,Thread 1
和 Thread 2
的输入都发生在退出之前:
Thread-0 enter synced
Thread-1 enter notsynced
Thread-0 exit synced
Thread-1 exit notsynced
您可以阅读JLS 17以获得正式的解释,但简而言之,只有一个线程可以进入同一个对象监视器上的同步块。
顺便说一句,我使用了 Thread.sleep
because(强调我的):
Thread.sleep causes the currently executing thread to sleep (temporarily cease execution) for the specified duration, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors, and resumption of execution will depend on scheduling and the availability of processors on which to execute the thread.