Objectクラスのwait、notify、およびnotifyAllメソッドが呼び出されて返されるたびに、コンソールに出力しようとしています。これを実現するために、ロックオブジェクトに代わってwait、notifyおよびnotifyAllメソッドを呼び出すラッパークラスを作成しました。その後、wait、notify、notifyAllの代わりにラッパーのメソッドを使用します。ここでは、この時私の最高の試み、一つはRunnableをR1wait()とnotify()メソッドをログに記録するより良い方法を探しています
Runnable r1 = new Runnable() {
@Override
public void run() {
synchronized (lock) {
try {
// wait not wrapped in while loop for brevity.
//lock.wait();
lock.objWrapper.waitNew(); // Use the wrapper method instead of wait()
} catch (InterruptedException e) {}
}
}
};
スレッド二つの実験のRunnable R2
Runnable r2 = new Runnable() {
@Override
public void run() {
synchronized(lock) {
//lock.notifyAll();
lock.objWrapper.notifyAllNew(); // Use the wrapper method instead of notifyAll()
}
}
};
クラスロックが
public class Lock {
ObjWrapper objWrapper = new ObjWrapper(this);
// shared data here
}
Lock lock = new Lock();
、として定義されているが実行されます
スレッドですラッパークラスは、
public class ObjWrapper {
Object obj = null;
ObjWrapper(Object obj) {
System.out.println("New Object wrapper created: Thread: " + Thread.currentThread() + " at time: " + Instant.now());
this.obj = obj;
}
public void waitNew() throws InterruptedException {
System.out.println("Entering Object::wait: Thread: " + Thread.currentThread() + " at time: " + Instant.now());
obj.wait();
System.out.println("Exiting Object::wait: Thread: " + Thread.currentThread() + " at time: " + Instant.now());
}
public void notifyNew() {
System.out.println("Entering Object::notify: Thread: " + Thread.currentThread() + " at time: " + Instant.now());
obj.notify();
System.out.println("Exiting Object::notify: Thread: " + Thread.currentThread() + " at time: " + Instant.now());
}
public void notifyAllNew() {
System.out.println("Entering Object::notifyAll: Thread: " + Thread.currentThread() + " at time: " + Instant.now());
obj.notifyAll();
System.out.println("Exiting Object::notifyAll: Thread: " + Thread.currentThread() + " at time: " + Instant.now());
}
}
そして最後に、私の質問は、
- ある
New Object wrapper created: Thread: Thread[main,5,main] at time: 2017-03-19T22:48:28.771Z Entering Object::wait: Thread: Thread[Thread One,5,main] at time: 2017-03-19T22:48:28.844Z Entering Object::notifyAll: Thread: Thread[Thread Two,5,main] at time: 2017-03-19T22:48:31.845Z Exiting Object::notifyAll: Thread: Thread[Thread Two,5,main] at time: 2017-03-19T22:48:31.845Z Exiting Object::wait: Thread: Thread[Thread One,5,main] at time: 2017-03-19T22:48:31.845Z
、コンソールに出力、
Thread t1 = new Thread(r1); t1.setName("Thread One"); t1.start(); Thread t2 = new Thread(r2); t2.setName("Thread Two"); t2.start();
を使用してスレッドを開始することはやって、任意の他の良い方法はありますこの?
- ObjWrapperを使用しているときに破損するエッジケースはありますか?
編集: 私はオブジェクトをロックするためのより良い方法を探していません。具体的には、wait()
,notify()
、notifyAll()
というメソッドの呼び出しをログに記録するだけです。
お返事ありがとうございます。私の編集を見てください。私は特にObjectクラスの 'wait()'と 'notify()'メソッドに興味があり、呼び出されたときを知りたいと思っています。 – Omer
これらは呼び出したときに呼び出されます。そのため、ラッピング部分についてはわかりません。私はあなたがnotify/notifyAll呼び出しの後にスレッドが実行を開始するときにチェックしようとしていると思います。間違っていると私を修正します。これははるかに広いトピックです – Adonis
あなたは正しいです。状態を待機状態に、またはその逆にすることができる。私が持っているコードへの唯一のフックは、デバッグステートメントです。これらの制約を念頭に置いて、私は 'wait'と' notify'の呼び出しをログに記録します – Omer