Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions 03concurrency/0301/src/main/java/java0/conc0302/lock/Count.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

package java0.conc0302.lock;

import java.util.concurrent.locks.ReentrantLock;

public class Count {

final ReentrantLock lock = new ReentrantLock();

public void get() {
// final ReentrantLock lock = new ReentrantLock();
try {
lock.lock();
System.out.println(Thread.currentThread().getName() + " get begin");
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + " get end");
lock.unlock();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public void put() {
// final ReentrantLock lock = new ReentrantLock();
try {
lock.lock();
System.out.println(Thread.currentThread().getName() + " put begin");
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + " put end");
lock.unlock();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
35 changes: 35 additions & 0 deletions 03concurrency/0301/src/main/java/java0/conc0302/lock/Count2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

package java0.conc0302.lock;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Count2 {

private final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();

public void get() {
rwLock.readLock().lock();
try {
System.out.println(Thread.currentThread().getName() + " get begin");
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + " get end");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
rwLock.readLock().unlock();
}
}

public void put() {
rwLock.writeLock().lock();
try {
System.out.println(Thread.currentThread().getName() + " put begin");
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + " put end");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
rwLock.writeLock().unlock();
}
}
}
40 changes: 40 additions & 0 deletions 03concurrency/0301/src/main/java/java0/conc0302/lock/Count3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

package java0.conc0302.lock;

public class Count3 {

private byte[] lock1 = new byte[1];
private byte[] lock2 = new byte[1];

public int num = 0;

public void add() {
synchronized (lock1) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock2) {
num += 1;
}
System.out.println(Thread.currentThread().getName() + "_" + num);
}
}

public void lockMethod() {
synchronized (lock2) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock1) {
num += 1;
}
System.out.println(Thread.currentThread().getName() + "_" + num);
}
}


}
18 changes: 18 additions & 0 deletions 03concurrency/0301/src/main/java/java0/conc0302/lock/LockMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

package java0.conc0302.lock;

public class LockMain {

public static void main(String[] args) {
Count3 count3 = new Count3();
ThreadA threadA = new ThreadA(count3);
threadA.setName("线程A");
threadA.start();

ThreadB threadB = new ThreadB(count3);
threadB.setName("线程B");
threadB.start();

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

package java0.conc0302.lock;

import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ObjectCache<T> {

public interface ObjectFactory<T> {
T makeObject();
}

class Node {
T obj;
Node next;
}

final int capacity;
final ObjectFactory<T> factory;
final Lock lock = new ReentrantLock();
final Semaphore semaphore;
private Node head;
private Node tail;

public ObjectCache(int capacity, ObjectFactory<T> factory) {
this.capacity = capacity;
this.factory = factory;
this.semaphore = new Semaphore(this.capacity);
this.head = null;
this.tail = null;
}

public T getObject() throws InterruptedException {
semaphore.acquire();
return getNextObject();
}

private T getNextObject() {
lock.lock();
try {
if (head == null) {
return factory.makeObject();
} else {
Node ret = head;
head = head.next;
if (head == null) tail = null;
ret.next = null;//help GC
return ret.obj;
}
} finally {
lock.unlock();
}
}

private void returnObjectToPool(T t) {
lock.lock();
try {
Node node = new Node();
node.obj = t;
if (tail == null) {
head = tail = node;
} else {
tail.next = node;
tail = node;
}

} finally {
lock.unlock();
}
}

public void returnObject(T t) {
returnObjectToPool(t);
semaphore.release();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

package java0.conc0302.lock;

public class ReentrantLockDemo {

public static void main(String[] args) {
final Count count = new Count();

for (int i = 0; i < 2; i++) {
new Thread() {
public void run() {
count.get();
}
}.start();
}

for (int i = 0; i < 2; i++) {
new Thread() {
public void run() {
count.put();
}
}.start();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

package java0.conc0302.lock;

public class ReentrantReadWriteLockDemo {

public static void main(String[] args) {
final Count2 count = new Count2();

for (int i = 0; i < 5; i++) {
new Thread() {
public void run() {
count.get();
}
}.start();
}

for (int i = 0; i < 5; i++) {
new Thread() {
public void run() {
count.put();
}
}.start();
}
}

/**
*
* Thread-0 get begin
Thread-1 get begin
Thread-2 get begin
Thread-3 get begin
Thread-4 get begin
Thread-0 get end
Thread-1 get end
Thread-2 get end
Thread-4 get end
Thread-3 get end
Thread-5 put begin
Thread-5 put end
Thread-6 put begin
Thread-6 put end
Thread-7 put begin
Thread-7 put end
Thread-8 put begin
Thread-8 put end
Thread-9 put begin
Thread-9 put end
*
*/
// 读锁不互斥、写锁互斥
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

package java0.conc0302.lock;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReentrantReadWriteLockDemo2 {

private final Map<String, Object> map = new HashMap<>();

private final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();

public Object readWrite(String key) {
Object value = null;
System.out.println("1.首先开启读锁去缓存中取数据");
rwLock.readLock().lock();
try {
value = map.get(key);
if (value == null) {
System.out.println("2.数据不存在,则释放读锁,开启写锁");
rwLock.readLock().unlock();
rwLock.writeLock().lock();
try {
if (value == null) {
value = "aaaa";
}
} finally {
System.out.println("3.释放写锁");
rwLock.writeLock().unlock();
}
System.out.println("4.开启读锁");
rwLock.readLock().lock();
}
} finally {
System.out.println("5.释放读锁");
rwLock.readLock().unlock();
}
return value;
}

public static void main(String[] args) {
ReentrantReadWriteLockDemo2 demo2 = new ReentrantReadWriteLockDemo2();
demo2.readWrite("wangwei");
}

}
15 changes: 15 additions & 0 deletions 03concurrency/0301/src/main/java/java0/conc0302/lock/ThreadA.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

package java0.conc0302.lock;

public class ThreadA extends Thread {
private Count3 count3;

public ThreadA(Count3 count3) {
this.count3 = count3;
}

public void run() {
count3.add();
}

}
15 changes: 15 additions & 0 deletions 03concurrency/0301/src/main/java/java0/conc0302/lock/ThreadB.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

package java0.conc0302.lock;

public class ThreadB extends Thread {
private Count3 count3;

public ThreadB(Count3 count3) {
this.count3 = count3;
}

public void run() {
count3.lockMethod();
}

}
Loading