https://blog.youkuaiyun.com/jackfrued/article/details/44921941
一.使用wait()和notify()实现Java多线程通信:两个线程交替打印A和B,如ABABAB
public class Test {
public static void main(String[] args) {
final PrintAB print = new PrintAB();
new Thread(new Runnable() {
public void run(){
for(int i=0;i<5;i++) {
print.printA();
}
}
}).start();
new Thread(new Runnable() {
public void run() {
for(int i=0;i<5;i++) {
print.printB(); }
}
}).start();
}
}
class PrintAB{
private boolean flag = true;
public synchronized void printA () {
while(!flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
} }
System.out.print("A");
flag = false;
this.notify();
}
public synchronized void printB () {
while(flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print("B");
flag = true;
this.notify(); }
}
二.实现一个线程安全的单例模式
在了解了单例模式的实现思路后,我们不难写出如下代码:
public class Singleton{
private static Singleton instance;
private Singleton(){} //将构造方法私有
public static Singleton getInstance(){
if(instance == null)
instance = new Singleton();
return instance;
}
}
以上代码即实现了一个懒汉式的单例模式,但它是线程不安全的。当有多个线程同时访问,那么就会创建多个实例。此时可以对getInstance方法加锁:
public static synchronized Singleton getInstance(){
if(instance == null)
instance = new Singleton();
return instance;
}
这样每次只允许一个线程调用该方法,那么就可以实现线程的安全性,但是也正由于每次只允许一个线程访问,该方法的效率十分低下,但是同步操作只需要在第一次调用时才被需要,即第一次创建单例实例对象时。
所以就引出了双重检验锁:
双重检验锁
双重检验锁模式(double checked locking pattern),是一种使用同步块加锁的方法。程序员称其为双重检查锁,因为会有两次检查 instance ==null,一次是在同步块外,一次是在同步块内。为什么在同步块内还要再检验一次?因为可能会有多个线程一起进入同步块外的if,如果在同步块内不进行二次检验的话就会生成多个实例了。
实现代码如下:
public static Singleton getInstance(){
if(instance == null){ //Single Checked
synchronized(Singleton.class){
if(instance == null) //Double Checked
instance = new Singleton();
}
}
return instance;
}
三.String类的intern()方法 :https://blog.youkuaiyun.com/soonfly/article/details/70147205
lock和synchronized的同步区别
区别如下:
1. lock是一个接口,而synchronized是java的一个关键字,synchronized是内置的语言实现;(具体实现上的区别在《Java虚拟机》中有讲解底层的CAS不同,以前有读过现在又遗忘了。)
2. synchronized在发生异常时候会自动释放占有的锁,因此不会出现死锁;而lock发生异常时候,不会主动释放占有的锁,必须手动unlock来释放锁,可能引起死锁的发生。(所以最好将同步代码块用try catch包起来,finally中写入unlock,避免死锁的发生。)
3. lock等待锁过程中可以用interrupt来终端等待,而synchronized只能等待锁的释放,不能响应中断;
4. lock可以通过trylock来知道有没有获取锁,而synchronized不能;
5. Lock可以提高多个线程进行读操作的效率。(可以通过readwritelock实现读写分离)
shiro理解:https://blog.youkuaiyun.com/qq_28525573/article/details/79101540