类A在Java中有两个同步方法,类B有两个静态同步方法,是否允许两个线程访问这两个场景
如果我们创建另一个线程t1并访问ThreadTest.m2;在这里面?是的,这是允许的,为什么这是静态的和it类级别的。但是如果我们有非静态方法,那么线程1和线程2不允许访问该方法请检查以下内容: 问候类A在Java中有两个同步方法,类B有两个静态同步方法,是否允许两个线程访问这两个场景,java,synchronization,thread-safety,Java,Synchronization,Thread Safety,如果我们创建另一个线程t1并访问ThreadTest.m2;在这里面?是的,这是允许的,为什么这是静态的和it类级别的。但是如果我们有非静态方法,那么线程1和线程2不允许访问该方法请检查以下内容: 问候 编辑:特别是这个答案:您所说的允许或不允许访问该方法是什么意思?您是指在尝试调用m2时线程是否被阻塞? public class ThreadTest { public static synchronized void m2() { System.out.printl
编辑:特别是这个答案:您所说的允许或不允许访问该方法是什么意思?您是指在尝试调用m2时线程是否被阻塞?
public class ThreadTest {
public static synchronized void m2() {
System.out.println("static sync m2");
System.out.println("current"+Thread.currentThread());
try { Thread.sleep(2000); }
catch (InterruptedException ie) {}
}
public static void main(String[] args) throws InterruptedException {
Thread t3 = new Thread() {
public void run() {
ThreadTest.m2();
}
};
t3.start();
t3.sleep(2000);
Thread.sleep(500); // which thread are we sleeping here?
System.out.println("t3.getState"+t3.getState());
}
}