import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Driver {
static Lock lock = new ReentrantLock();
static Integer incr = 20;
public static void main(String [] arg) throws InterruptedException
{
Thread thr1 = new Thread(new Runnable(){
@Override
public void run() {
lock.lock();
System.out.println("Gained access! - 1");
try {Thread.sleep(5000);} catch (InterruptedException e) {}
incr++;
lock.unlock();
}
});
Thread thr2 = new Thread(new Runnable(){
@Override
public void run() {
lock.lock();
System.out.println("Gained access! - 2");
incr++;
lock.unlock();
}
});
Thread thr3 = new Thread(new Runnable(){
@Override
public void run() {
synchronized(incr){
System.out.println("Gained access! - 3");
try {Thread.sleep(5000);} catch (InterruptedException e) {}
incr++;
}
}
});
Thread thr4 = new Thread(new Runnable(){
@Override
public void run() {
synchronized(incr){
System.out.println("Gained access! - 4");
incr++;
}
}
});
thr1.start();
thr2.start();
thr3.start();
thr4.start();
thr1.join();
thr2.join();
thr3.join();
thr4.join();
System.out.println(incr);
}
}
Gained access! - 3
Gained access! - 2
Gained access! - 1
Gained access! - 4
23
Gained access! - 1
Gained access! - 4
Gained access! - 3
Gained access! - 2
24
Switching orders of Thread execution. Seen it hit a sum of 22.
我正在尝试进行简单的可重入锁定和同步练习。
ReentrantLock
内部正在使用synchronized
?while
循环,直到变量可访问或执行实际暂停(在字节码中),直到对象/锁定被解锁?为什么上面的总和不总是24?线程执行以下操作:
主题1:
Lock
| 5s等待|值++
线程2:Lock
| 0s等待|值++
主题3:syncronized
| 5s等待|值++
主题4:syncronized
| 0s等待|值++
synchronized
内部正在做什么吗?我对synchronized
内部运作的了解很少。这是我认为可能是复制synchronized
的合理方式。所以,我不知道这是不是正确的方法。所以请告诉我Java中的内部工作方式是否与此不同。public class Driver {
static Safe<Integer> value = new Safe<Integer>(20);
public static void main(String [] arg) throws InterruptedException
{
Thread thr1 = new Thread(new Runnable(){
@Override
public void run() {
value.lock();
System.out.println("Gained access! - 1");
value.data++;
try {Thread.sleep(5000);} catch (InterruptedException e) {}
value.unlock();
}
});
Thread thr2 = new Thread(new Runnable(){
@Override
public void run() {
value.lock();
System.out.println("Gained access! - 2");
value.data++;
try {Thread.sleep(5000);} catch (InterruptedException e) {}
value.unlock();
}
});
thr1.start();
thr2.start();
thr1.join();
thr2.join();
System.out.println(value);
}
}
class Safe<E>{
private volatile boolean lock = false;
protected E data;
public Safe(E d)
{
data = d;
}
public String toString()
{
return data.toString();
}
public void lock()
{
while(isLocked()){}
lock = true;
}
public void unlock()
{
lock = false;
}
public boolean isLocked()
{
return lock;
}
}
谢谢!
答案 0 :(得分:1)
synchronized
statement uses monitorenter
and monitorexit
,它使用this
(或其编码)作为条件变量,而后者又与您的ReentrantLock
不同。线程总是以不同顺序执行的原因仅仅在于执行顺序取决于系统调度程序。线程排队等待创建的顺序通常不是它们第一次运行的顺序。synchronized
和lock()
锁定了不同的信号量。具体来说,线程1和2彼此同步,但不与3和4同步。因为你有两个同步的增量,你总是会看到至少两个增量(因此总和至少是22)。