Driver.java
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);
}
}
Run 1 - Output
Gained access! - 3
Gained access! - 2
Gained access! - 1
Gained access! - 4
23
Run 2 - Output
Gained access! - 1
Gained access! - 4
Gained access! - 3
Gained access! - 2
24
Run N - Output
Switching orders of Thread execution. Seen it hit a sum of 22.
Questions
I am attempting to do a simple Reentrant lock and syncrhonized practice.
- Do these outputs indicate that
ReentrantLock
is usingsynchronized
internally? - Are these locks/synchronized basically
while
loops until a variable is accessible or is execution actually paused (in bytecode) until Objects/Locks are unlocked? Why isn't the sum always 24 in case above? The threads do the following:
Thread 1:
Lock
| 5s wait | value++
Thread 2:Lock
| 0s wait | value++
Thread 3:syncronized
| 5s wait | value++
Thread 4:syncronized
| 0s wait | value++
4. Can someone confirm if this basically what synchronized
is doing internally? My knowledge of the internal workings of synchronized
is minimal. This is something I thought might be a logical way to replicate synchronized
. So, again I am not sure if this is the right way. So please let me know if the internal workings in Java are different from this.
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;
}
}
Thank you!