Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Oncurrent?取决于同步的内容。如果您执行同步(此),则应用完全相同的规则。 public class Test { public synchronized void m1() { try { Thread.sleep(20_Java_Multithreading_Synchronized - Fatal编程技术网

Java Oncurrent?取决于同步的内容。如果您执行同步(此),则应用完全相同的规则。 public class Test { public synchronized void m1() { try { Thread.sleep(20

Java Oncurrent?取决于同步的内容。如果您执行同步(此),则应用完全相同的规则。 public class Test { public synchronized void m1() { try { Thread.sleep(20,java,multithreading,synchronized,Java,Multithreading,Synchronized,Oncurrent?取决于同步的内容。如果您执行同步(此),则应用完全相同的规则。 public class Test { public synchronized void m1() { try { Thread.sleep(2000); } catch (InterruptedException ie) {} } public synchronized void m2() { try { Thread.sleep(20


Oncurrent?取决于同步的内容。如果您执行同步(此),则应用完全相同的规则。
public class Test {

    public synchronized void m1() {
        try { Thread.sleep(2000); }
        catch (InterruptedException ie) {}
    }

    public synchronized void m2() {
        try { Thread.sleep(2000); }
        catch (InterruptedException ie) {}
    }

    public static void main(String[] args) throws InterruptedException {
        final Test t = new Test();
        Thread t1 = new Thread() { public void run() { t.m1(); } };
        Thread t2 = new Thread() { public void run() { t.m2(); } };

        t1.start();
        Thread.sleep(500);

        t2.start();
        Thread.sleep(500);

        System.out.println(t2.getState());
    }
}
BLOCKED
// Thread 1
A a1 = new A();
a1.m1();

// Thread 2
A a2 = new A();
a2.m2()