import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class TableControl implements Runnable {
private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();;
private int i = 0;
private void add(int v) {
writeLock().lock();
i += v;
System.out.println(System.currentTimeMillis()+" add"+i);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
writeLock().unlock();
}
private void get() {
readLock().lock();
System.out.println(System.currentTimeMillis()+" get"+i);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
readLock().unlock();
}
public Lock readLock() {
return readWriteLock.readLock();
}
public Lock writeLock() {
return readWriteLock.writeLock();
}
@Override
public void run() {
get();
add(100);
}
}
public class TestMain {
public static void main(String args[]){
TableControl tc=new TableControl();
Thread t1 =new Thread(tc);
Thread t2 =new Thread(tc);
t1.start();
t2.start();
}
}