import java.util.concurrent.locks.*;
import java.util.*;
public class ReadWriteLockTest {
public static void main(String[] args) throws InterruptedException {
Counter c = new Counter();
var ts = new ArrayList<Thread>();
for (int i = 1; i < 5; i++) {
final String thNo = "Thread_" + Integer.toString(i + 1);
// read
Thread t = new Thread(() -> {
while (true) {
try {
Thread.sleep(200 + (int) (Math.random() * 10));
System.out.println(thNo + " duplicate read:" + Arrays.toString(c.get(thNo)));
} catch (InterruptedException e) {
System.out.println("stop task " + thNo);
return;
}
});
t.start();
ts.add(t);
for (int i = 0; i < 10; i++) {
final int index = i;
final String thNo = "Thread_" + (char) ('A' + index) + (index + 1);
Thread.sleep(200 + (int) (Math.random() * 400));
System.out.println(thNo + " write:" + c.inc(index, thNo));
Thread.sleep(2000);
for (var t : ts) {
t.interrupt();
class Counter {
private final ReadWriteLock rwlock = new ReentrantReadWriteLock();
private final Lock rlock = rwlock.readLock();
private final Lock wlock = rwlock.writeLock();
private int[] counts = new int[10];
public int inc(int index, String thNo) throws InterruptedException {
wlock.lock(); // 加写锁
System.out.print(thNo + " got wlock -> ");
try {//
counts[index] += 1;
Thread.sleep(10);
return counts[index];
} finally {
System.out.println(thNo + " releasing wlock...");
wlock.unlock(); // 释放写锁
public int[] get(String thNo) throws InterruptedException {
rlock.lock(); // 加读锁
System.out.print(thNo + " got rlock -> ");
try {// copy
Thread.sleep(20);
return Arrays.copyOf(counts, counts.length);
System.out.println(thNo + " releasing rlock...");
rlock.unlock(); // 释放读锁
Sign in to make a reply
Junes_99994
import java.util.concurrent.locks.*;
import java.util.*;
public class ReadWriteLockTest {
public static void main(String[] args) throws InterruptedException {
Counter c = new Counter();
var ts = new ArrayList<Thread>();
for (int i = 1; i < 5; i++) {
final String thNo = "Thread_" + Integer.toString(i + 1);
// read
Thread t = new Thread(() -> {
while (true) {
try {
Thread.sleep(200 + (int) (Math.random() * 10));
System.out.println(thNo + " duplicate read:" + Arrays.toString(c.get(thNo)));
} catch (InterruptedException e) {
System.out.println("stop task " + thNo);
return;
}
}
});
t.start();
ts.add(t);
}
for (int i = 0; i < 10; i++) {
final int index = i;
final String thNo = "Thread_" + (char) ('A' + index) + (index + 1);
// read
Thread t = new Thread(() -> {
while (true) {
try {
Thread.sleep(200 + (int) (Math.random() * 400));
System.out.println(thNo + " write:" + c.inc(index, thNo));
} catch (InterruptedException e) {
System.out.println("stop task " + thNo);
return;
}
}
});
t.start();
ts.add(t);
}
Thread.sleep(2000);
for (var t : ts) {
t.interrupt();
}
}
}
class Counter {
private final ReadWriteLock rwlock = new ReentrantReadWriteLock();
private final Lock rlock = rwlock.readLock();
private final Lock wlock = rwlock.writeLock();
private int[] counts = new int[10];
public int inc(int index, String thNo) throws InterruptedException {
wlock.lock(); // 加写锁
System.out.print(thNo + " got wlock -> ");
try {//
counts[index] += 1;
Thread.sleep(10);
return counts[index];
} finally {
System.out.println(thNo + " releasing wlock...");
wlock.unlock(); // 释放写锁
}
}
public int[] get(String thNo) throws InterruptedException {
rlock.lock(); // 加读锁
System.out.print(thNo + " got rlock -> ");
try {// copy
Thread.sleep(20);
return Arrays.copyOf(counts, counts.length);
} finally {
System.out.println(thNo + " releasing rlock...");
rlock.unlock(); // 释放读锁
}
}
}