import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class UserInfo implements Runnable {
private Lock lock = new ReentrantLock();
@Override
public void run() {
getInfo();
}
//getInfo方法
private void getInfo() {
lock.lock();
try {
System.out.println(Thread.currentThread().getName() + "\tgetInfo");
setInfo();//调用setInfo方法
} finally {
lock.unlock();
}
}
//setInfo方法
private void setInfo() {
lock.lock();
try {
System.out.println(Thread.currentThread().getName() + "\tsetInfo");
} finally {
lock.unlock();
}
}
}
/**
* @description: 可重入锁的示例代码
* @author: xz
*/
public class LockDemo {
public static void main(String[] args) {
UserInfo userInfo=new UserInfo();
Thread t1 = new Thread(userInfo);
Thread t2 = new Thread(userInfo);
t1.start();
t2.start();
}
}
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
/**
* @description: 手写自旋锁示例代码
* @author: xz
*/
public class SpinLockDemo {
//原子引用线程
AtomicReference<Thread> atomicReference =new AtomicReference<>();
//加锁
public void myClock(){
Thread thread = Thread.currentThread();
System.out.println(Thread.currentThread().getName()+"\t come in ....加锁");
//初始时原子引用线程为空,当原子引用线程为空时,添加当前线程添
//如果返回true的话,直接进入while进行循环,所以取反,第一次不进入while循环
while (!atomicReference.compareAndSet(null,thread)){
}
}
//解锁
public void myUnClock(){
Thread thread = Thread.currentThread();
atomicReference.compareAndSet(thread,null);
System.out.println(Thread.currentThread().getName()+"\t go out ....解锁");
}
public static void main(String[] args) {
SpinLockDemo spinLockDemo = new SpinLockDemo();
//A线程,获取锁后睡眠5秒钟,再释放锁
new Thread(()->{
spinLockDemo.myClock();
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
spinLockDemo.myUnClock();
},"线程A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
//B线程,获取锁后直接释放锁
new Thread(()->{
spinLockDemo.myClock();
spinLockDemo.myUnClock();
},"线程B").start();
}
}
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* 资源类
*/
class MyResource{
/**
* 保证可见性
*/
private volatile HashMap<String, Object> map = new HashMap();
private ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
/**
* 写操作
*/
public void writeResoure(String key, Object value) {
reentrantReadWriteLock.writeLock().lock();
try {
System.out.println(Thread.currentThread().getName() + "\t正在写入" + key);
//模拟网络延时
try {
TimeUnit.MICROSECONDS.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
map.put(key, value);
System.out.println(Thread.currentThread().getName() + "\t正在完成");
} finally {
reentrantReadWriteLock.writeLock().unlock();
}
}
/**
* 读
*/
public void readResoure(String key) {
reentrantReadWriteLock.readLock().lock();
try {
System.out.println(Thread.currentThread().getName() + "\t正在读取");
//模拟网络延时
try {
TimeUnit.MICROSECONDS.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
Object result = map.get(key);
System.out.println(Thread.currentThread().getName() + "\t正在完成" + result);
} finally {
reentrantReadWriteLock.readLock().unlock();
}
}
}
/**
* @description: 多个线程同时操作一个资源类没有任何问题。 所以为了满足并发量,读取共享资源应该可以同时进行.
* 但是,如果有一个线程想去写共享资源,就不应该有其他线程可以对资源进行读或写.
* @author: xz
*/
public class ReadWriteLockDemo {
public static void main(String[] args) {
MyResource myResource = new MyResource();
for (int i = 1; i <= 3; i++) {
final int temp = i;
new Thread(() -> {
myResource.writeResoure(temp + "", temp);
}, "线程"+String.valueOf(i)).start();
}
for (int i = 1; i <= 3; i++) {
int finalI = i;
new Thread(() -> {
myResource.readResoure(finalI + "");
}, "线程"+String.valueOf(i)).start();
}
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://wwwxz.blog.csdn.net/article/details/122463525
内容来源于网络,如有侵权,请联系作者删除!