1、应用场景: 关闭机器 (Runtime通过查看源码就是单例模式)
2、应用场景: 在指定时间安排指定任务 (Timer)
/*
* 饿汉式和懒汉式的区别
* 1,饿汉式是空间换时间,懒汉式是时间换空间
* 2,在多线程访问时,饿汉式不会创建多个对象,而懒汉式有可能会创建多个对象
*/
/*
* 饿汉式
*/
class Singleton {
//1,私有构造方法,其他类不能访问该构造方法了
private Singleton(){}
//2,创建本类对象
private static Singleton s = new Singleton();
//3,对外提供公共的访问方法
public static Singleton getInstance() { //获取实例
return s;
}
}
/*
* 懒汉式,单例的延迟加载模式
*/
class Singleton {
//1,私有构造方法,其他类不能访问该构造方法了
private Singleton(){}
//2,声明一个引用
private static Singleton s ;
//3,对外提供公共的访问方法
public static Singleton getInstance() { //获取实例
if(s == null) {
//线程1等待,线程2等待
s = new Singleton();
}
return s;
}
}
/**
* 实际式 -- 实际的应用模式
*/
class Singleton {
//1,私有构造方法,其他类不能访问该构造方法了
private Singleton(){}
//2,声明一个引用
public static final Singleton s = new Singleton();
}
测试主类:
public class Test_Singleton {
/**
* @param args
* * 单例设计模式:保证类在内存中只有一个对象。
*/
public static void main(String[] args) {
//Singleton s1 = new Singleton();
Singleton s1 = Singleton.s; //成员变量被私有,不能通过类名.调用
//Singleton.s = null;
Singleton s2 = Singleton.s;
System.out.println(s1 == s2);
/* Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
System.out.println(s1 == s2);
*/
}
}
Runtime通过查看源码就是单例模式
1、单例模式应用场景: 关闭机器
public class Demo2_Runtime {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Runtime r = Runtime.getRuntime(); //获取运行时对象
//r.exec("shutdown -s -t 300");
r.exec("shutdown -a");
}
}
应用场景2: 在指定时间安排指定任务
public class Demo3_Timer {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
Timer t = new Timer();
//在指定时间安排指定任务
//第一个参数,是安排的任务,第二个参数是执行的时间,第三个参数是过多长时间再重复执行
t.schedule(new MyTimerTask(), new Date(188, 6, 1, 14, 22, 50),3000);
while(true) {
Thread.sleep(1000);
System.out.println(new Date());
}
}
}
class MyTimerTask extends TimerTask {
@Override
public void run() {
System.out.println("起床背英语单词");
}
}