👍 👎 💯 👏 🔔 🎁 ❓ 💣 ❤️ ☕️ 🌀 🙇 💋 🙏 💦 💩 ❗️ 💢
————————————————
文章目录
知识点
第三天总结
第四天
volatile关键字
对象等待集
wait的工作过程
代码块包裹快捷键
快捷键Ctrl + Alt + T
整理代码格式快捷键
快捷键Ctrl+ Alt + L
竟态条件问题
多线程的一些案例
单例模式
饿汉模式
public class ThreadDemo22 {
//先创建一个表示单例的类
//Singleton这个类只能有一个实例
//饿汉模式的单例实现,"饿"指的是,只要类被加载,实例就会立刻拆功能键(实例创建的时机比较早)
static class Singleton {
//把构造方法变成私有,此时在该类外部就无法new这个类的实例
private Singleton() {
}
//再来创建一个static的成员,表示Singleton类的唯一的实例
//Static 和 类相关,和实例无关,类在内存中只有一份,static成员也就只有一份
static Singleton instance = new Singleton();
public static Singleton getInstance() {
return instance;
}
}
public static void main(String[] args) {
//此处的getInstance就是获取该类实例的唯一方式,不应该使用其他方式来创建实例
Singleton s = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
System.out.println(s == s2);
}
}
懒汉模式
public class ThreadDemo23 {
//使用懒汉模式来实现,Singleton类被加载的时候,不会立刻实例化
//等到第一次使用这个实例的时候,再实例化
static class Singleton{
private Singleton(){}
private static Singleton instance = null;
public static Singleton getInstance()
{
if(instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
完美的懒汉模式代码
public class ThreadDemo23 {
//使用懒汉模式来实现,Singleton类被加载的时候,不会立刻实例化
//等到第一次使用这个实例的时候,再实例化
static class Singleton {
private Singleton() {
}
private volatile static Singleton instance = null;
//加锁就是为了代码块的原子性
static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
}
阻塞队列案例
生产者消费者模型
先赞后看,养成习惯!!!^ _ ^♥♥♥
每天都更新知识点哦!!!
码字不易,大家的支持就是我坚持下去的动力。点赞后不要忘记关注我哦!