import java.util.*;
import java.text.*;
import java.util.concurrent.locks.ReentrantLock;
public class lockexamplegr {
public static ReentrantLock thelock = new ReentrantLock();
public static int i = 0;
public static void main(String[] args)
{
new User( "Thread 1").start();
new User( "Thread 2").start();
}
}
class User extends Thread {
private String name="";
public User(String s){
name=s;
}
@Override
public void run()
{
// lockexample.thelock.lock();
try {
for (int j=0;j<5;j++) {
lockexamplegr.i++;
System.out.println("i="+lockexamplegr.i+" thread "+name+ " Time= "+new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date()));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
// lockexample.thelock.lock(); Just a note to demonstrate ReentrantLock, not relevant.
// lockexample.thelock.unlock();
}
} finally {
// lockexample.thelock.unlock();
}
}
}