package com.haizhitao.network;
public class SingletonTest
{
public static void main(String[] args)
{
new MyThread().start();
new MyThread().start();
}
}
class Singleton
{
private static Singleton singleton;
private Singleton()
{
}
public static Singleton getInstance()
{
if(null == singleton)
{
try
{
Thread.sleep((long)(Math.random() * 1000));
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
singleton = new Singleton();
}
return singleton;
}
}
class MyThread extends Thread
{
@Override
public void run()
{
System.out.println(Singleton.getInstance());
}
}
//output:
// com.haizhitao.network.Singleton@1542a75
// com.haizhitao.network.Singleton@af993e
78
最新推荐文章于 2023-09-02 01:38:03 发布