仅提供学习,侵权必删,如有错误,敬请告知
一、公平锁/非公平锁
package suo;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Gongpingsuo {
private static Lock lock = new ReentrantLock(true); //当为true是 是公平锁,默认是非公平锁
private static volatile int count = 100;
public static void lock() {
lock.lock(); //添加锁
count--;
System.out.println("剩余:"+count);
lock.unlock(); //释放锁
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(()-> {
lock();
}).start();
}
}
}
二、死锁
package suo;
public class Sisuo {
public static void AA() {
synchronized ("AA") {
System.out.println("AA");
// try {
// "AA".wait();
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
synchronized ("BB") {
System.out.println("AABB");
}
}
}
public static void BB() {
synchronized ("BB") {
System.out.println("BB");
synchronized ("AA") {
System.out.println("BBAA");
// "AA".notifyAll();
}
}
}
public static void main(String[] args) {
Thread AA = new Thread("AA") {
@Override
public void run() {
// TODO Auto-generated method stub
AA();
}
};
Thread BB = new Thread("BB") {
@Override
public void run() {
// TODO Auto-generated method stub
BB();
}
};
AA.start();
BB.start();
}
}
三、悲观锁
package Suo9;
public class Beiguansuo {
private static volatile int count = 100;
public static synchronized void temp() {
count--;
System.out.println("剩余:"+count);
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(()-> {
temp();
}).start();
}
}
}
四、乐观锁
package Suo9;
import java.util.concurrent.atomic.AtomicInteger;
public class Leguansuo {
private static AtomicInteger atomicInteger = new AtomicInteger(0);
public static void Add() {
atomicInteger.incrementAndGet();
}
public static void Del() {
atomicInteger.decrementAndGet();
}
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 100; i++) {
Thread thread = new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
Add();
}
};
thread.start();
thread.join();
}
for (int i = 0; i < 100; i++) {
Thread thread = new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
Del();
}
};
thread.start();
thread.join();
}
System.out.println("当前值:"+atomicInteger.get());
}
}
五、同步锁
package Suo8;
public class Tongbusuo {
private static int count = 100;
public synchronized static void name() {
count--;
System.out.println("剩余数:"+count);
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
Thread thread = new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
name();
}
};
thread.start();
}
}
}
六、互斥锁
package Suo9;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Huchisuo {
private static Lock lock = new ReentrantLock();
private static volatile int count = 2;
public static void temp() {
lock.lock();
count--;
System.out.println("剩余:"+count);
lock.unlock();
}
public static void main(String[] args) {
Thread t1 = new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
temp();
}
};
Thread t2 = new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
temp();
}
};
t1.start();
t2.start();
}
}
七、可重入锁
package Suo9;
public class Kechongrusuo {
public static void AA() {
System.out.println("AA");
BB();
}
public static void BB() {
System.out.println("BB");
}
public static void main(String[] args) {
Thread thread = new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
AA();
}
};
thread.start();
}
}
八、读写锁
package Suo9;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class Duxiesuo {
private static ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private static Map<String, Object> map = new ConcurrentHashMap<String, Object>();
public static void Xie(String Keys,String Value) {
lock.writeLock().lock();
System.out.println("我正在写:"+Keys);
map.put(Keys, Value);
System.out.println("我写完了!下一个");
lock.writeLock().unlock();
}
public static void Du(String Keys) {
lock.readLock().lock();
System.out.println("我正在读取文件");
System.out.println("我读完了:"+map.get(Keys));
lock.readLock().unlock();
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
int temp = i ;
Thread thread = new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
Xie(temp+"",temp+"");
}
};
thread.start();
}
for (int i = 0; i < 10; i++) {
int temp = i ;
Thread thread = new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
Du(temp+"");
}
};
thread.start();
}
}
}
九、自旋锁
package Suo9;
import java.util.concurrent.atomic.AtomicReference;
public class Zixuansuo {
private static AtomicReference<Thread> atomicReference = new AtomicReference<Thread>();
public static void lock() throws InterruptedException {
Thread thread = Thread.currentThread();
System.out.println(thread.getName());
while (!atomicReference.compareAndSet(null, thread)) {
System.out.println(thread.getName()+"正在自我循环访问");
Thread.sleep(1000);
}
}
public static void unlock() {
Thread thread = Thread.currentThread();
atomicReference.compareAndSet(thread, null);
System.out.println(thread.getName()+"我已释放");
}
public static void main(String[] args) {
Thread AA = new Thread(()-> {
try {
lock();
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
unlock();
},"AA");
AA.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thread BB = new Thread(()-> {
try {
lock();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
unlock();
},"BB");
BB.start();
}
}