可重入鎖?
reentrantlock是獨占鎖且可重入的?synchronized 也可以重入?
?可重入意思就是這個線程已經獲取鎖了,你再獲取該鎖還能獲取 獲取的還是原來的鎖 不會出現問題 可以降低編程難度
代碼如下:
new Thread(new Runnable() {@Overridepublic void run() {synchronized (this) {System.out.println("第1次獲取鎖,這個鎖是:" + this);int index = 1;while (true) {synchronized (this) {System.out.println("第" + (++index) + "次獲取鎖,這個鎖是:" + this);}if (index == 10) {break;}}}} }).start();
?