It's just an experiment Retrieve Lock
object from ArrayBlockingQueue
using reflection. Then call lock.lock()
and spawn a thread which will try to add a value to queue. I just don't understand why spawned thread successfully calls lock()
and adds the value to the queue.
CODE:
ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<>(10);
Field lockField = ArrayBlockingQueue.class.getDeclaredField("lock");
lockField.setAccessible(true);
ReentrantLock lock = (ReentrantLock) lockField.get(queue);
lock.lock();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
queue.add(1);
}
});
t.start();
Thread.sleep(2000);
System.out.println(queue.take());
Actual: output 1
Expected: hanging after queue.take()
call
Please explain such behavior
Aucun commentaire:
Enregistrer un commentaire