package com.study.lock.locks4;
public class Test_ReadWriteLock {
static JamesReadWriteLock rwLock = new JamesReadWriteLock();
//static ReadWriteLock rwLock = new ReentrantReadWriteLock();
static volatile int i =0;
static void add(){
i++;
}
public static void main(String args[]) throws InterruptedException {
long startTime = System.currentTimeMillis();
for (int a=1; a<=20000; a++){
final int n = a;
new Thread(new Runnable() {
@Override
public void run() {
if (n%5 ==0){
rwLock.lock();
add();
rwLock.unLock();
}else{
rwLock.lockShared();
System.out.println("i=" +i);
int a = i;
rwLock.unLockShared();
}
}
}).start();
}
while (true){
System.out.println("目前耗时:" + (System.currentTimeMillis()-startTime) /1000 + "s");
Thread.sleep(1000L);
System.out.println("i=" + i);
}
}
}
package com.study.lock.locks4;
public class Test_ReentarntLock {
volatile static int i=0;
static JamesReentrantLock lc = new JamesReentrantLock(true);
public static void add(){
lc.lock();
i++;
lc.unLock();
}
public static void main(String args[]) throws InterruptedException {
for (int i=0; i<10; i++){
new Thread(){
@Override
public void run() {
for (int j=0; j< 10000; j++){
//System.out.println(currentThread().getName()+ "....");
add();
}
System.out.println("done...");
}
}.start();
}
Thread.sleep(6000L);
System.out.println(i);
}
}