今日内容
一、Java锁机制
1.Synchronized重入锁
public class Test001 implements Runnable {
@Override
public void run() {
set();
}
public synchronized void set() {
System.out.println("name:" + Thread.currentThread().getName() + " set();");
get();
}
public synchronized void get() {
System.out.println("name:" + Thread.currentThread().getName() + " get();");
}
public static void main(String[] args) {
Test001 test001 = new Test001();
Thread t1 = new Thread();
t1.start();
}
}
2.ReentrantLock重入锁
// 演示Lock锁是否具备 可重入性 特征:锁可以传递(方法递归传递)
public class Test02 extends Thread {
Lock lock = new ReentrantLock();
@Override
public void run() {
set();
}
public void set() {
try {
lock.lock();
System.out.println(Thread.currentThread().getId());
get();
} catch (Exception e) {
} finally {
lock.unlock();
}
}
public void get() {
try {
lock.lock();
System.out.println(Thread.currentThread().getId());
} catch (Exception e) {
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
Test02 Test02 = new Test02();
Test02.start();
}
}
3.ReentrantReadWriteLock读写锁
package com.itmayiedu;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
public class Cache {
private ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
private volatile Map<String, String> map = new HashMap<>();
private ReadLock readLock = rwl.readLock();
private WriteLock writeLock = rwl.writeLock();
public String get(String key) {
try {
readLock.lock();
System.out.println("正在做读的操作,key:" + key + "开始..");
Thread.sleep(100);
String value = map.get(key);
System.out.println("正在做读的操作,key:" + key + "结束..");
return value;
} catch (Exception e) {
return null;
}finally {
readLock.unlock();
}
}
public void put(String key, String value) {
try {
writeLock.lock();
System.out.println("正在做写的操作,key:" + key + ",value:" + value + "开始..");
Thread.sleep(100);
map.put(key, value);
System.out.println("正在做写的操作,key:" + key + ",value:" + value + "結束..");
} catch (Exception e) {
}finally {
writeLock.unlock();
}
}
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
Cache.put(i + "", "i:" + i);
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
Cache.get(i + "");
}
}
}).start();
}
}
4.乐观锁
5.悲观锁
6.AtomicInteger原子类
public class Test0001 implements Runnable {
// private static Integer count = 1;
private static AtomicInteger atomic = new AtomicInteger();
@Override
public void run() {
while (true) {
int count = getCountAtomic();
System.out.println(count);
if (count >= 150) {
break;
}
}
}
/*
public synchronized Integer getCount() {
try {
Thread.sleep(50);
} catch (Exception e) {
// TODO: handle exception
}
return count++;
}
*/
public Integer getCountAtomic() {
try {
Thread.sleep(50);
} catch (Exception e) {
// TODO: handle exception
}
return atomic.incrementAndGet();
}
public static void main(String[] args) {
Test0001 test0001 = new Test0001();
Thread t1 = new Thread(test0001);
Thread t2 = new Thread(test0001);
t1.start();
t2.start();
}
}