同期メソッドまたは同期ブロックを使用して異なる結果を生成するシナリオを観察しました。コードの下から :私はCallmeのための同期方法を使用する場合同期メソッドとブロックの動作が異なる
[thread1]
[thread1]
[thread3]
[thread3]
[thread2]
[thread2]
しかし:
class Callme {
void call(String msg) {
System.out.print("[" + msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("]");
}
}
class Caller implements Runnable{
String msg;
Callme target;
Thread t;
public Caller(Callme target, String msg) {
this.target = target;
this.msg = msg;
t = new Thread(this, "Caller thread");
t.start();
}
@Override
public void run() {
synchronized(target) {
target.call(msg);
new Callme().call(msg);
}
}
}
public class Test {
public static void main(String[] args) throws InterruptedException {
Callme obj = new Callme();
new Caller(obj, "thread1");
new Caller(obj, "thread2");
new Caller(obj, "thread3");
Thread.currentThread().join();
}
}
私は出力に含まを以下のように同期されている発信者:: runメソッドにsynchronizedブロックを使用します。 :callメソッドは、代わりに同期ブロックの、出力が同期していない:
[thread1]
[thread1[thread2]
]
[thread3[thread2]
]
[thread3]
私の期待は、出力は両方のケースのBECAに同期させるべきではないです私は "Callme :: call"メソッドを呼び出すときに別のオブジェクトを使用しています
これは私に同期ブロックの概念についての質問になりましたか?