JAVA-----鎖機制

僅提供學習,侵權必刪,如有錯誤,敬請告知

一、公平鎖/非公平鎖

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();
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章