62,630
社区成员
发帖
与我相关
我的任务
分享
//饿汉式
public class EagerSingleton
{
//一上来就创建实例,所以叫饿汉
private static final EagerSingleton m_instance =
new EagerSingleton();
private EagerSingleton() { }
public static EagerSingleton getInstance()
{
return m_instance;
}
}
//懒汉式
public class LazySingleton
{
private static LazySingleton
m_instance = null;
private LazySingleton() { }
synchronized public static LazySingleton getInstance(){
//实例为空的时候才创建,否者直接拿来用,所以是懒汉
if (m_instance == null)
{
m_instance = new LazySingleton();
}
return m_instance;
}
}
public class SingleClass{
private static SingleClass instance;
protected SingleClass(){}
public static SingleClass GetInstance() {
if(instance == null)
{
instance = new SingleClass();
}
return instance;
}
}
try {
ServerSocket server = new ServerSocket(9000);
} catch (IOException e) {
// TODO 自动生成 catch 块
System.out.println("端口被占用!");
}
try{
ServerPort port=new ServerPort(9000);}
catch(IOException e){
}