public class A{
private static A singleton;
private A(){}
public A PrevInstance(){
if(singleton == null){
singleton = new A();
}
return singleton;
}
}
private static A singleton;
private A(){}
public A PrevInstance(){
if(singleton == null){
singleton = new A();
}
return singleton;
}
}
private static ConnectionPool m_ConnectionPoolInstance; //唯一实例 //构造函数私有以防止通过构造函数实例化
private ConnectionPool() {
//do something...
} public static ConnectionPool getInstance() { //判断是否已经存在实例
if (!m_bIntanceFlag) {
m_bIntanceFlag = true;
m_ConnectionPoolInstance = new ConnectionPool();
}
else {
//存在实例返回NULL
m_ConnectionPoolInstance = null;
}
return m_ConnectionPoolInstance; } public void finalize() {
//析构时标志复位
m_bIntanceFlag = false;
m_ConnectionPoolInstance = null;
} //测试
public static void main(String args[]) { ConnectionPool oConnPool1, oConnPool2; //构造第一个连接池对象
oConnPool1 = ConnectionPool.getInstance();
if (oConnPool1 != null)
System.out.println("获得第一个连接池对象成功!"); //构造第二个连接池对象
oConnPool2 = ConnectionPool.getInstance();
if (oConnPool2 == null)
System.out.println("获得第二个连接池对象失败!"); }
}
synchronized
singleton模式加上同步标记synchronized可以在运行时时保证只生成一个实例,我想要实现的是:
例如,Sample.class正在运行的时候,如果我再一次执行java Sample,这样就有两个Sample.class在运行,如何避免这一现象。在VB中,我可以通过app.PrevInstance 来判断,如果app.PrevInstance=True直接退出就是了。Java中如何实现这种功能。http://www.mhdn.net/p/2003-01-12/6430.html 这里有一个通过端口占用来判断的例子,不过在系统中占用端口不太好。
public class singletonClass () {
private static final singletonClass INSTANCE = new singletonClass();
private singletonClass () {}
public static getInstance () {
return INSTANCE;
}
}
2. serialize this Object, store it on your local disk.
3. main() within your app: read and write this flag. if flag true, exit prog.
in order to avid adding synchronize code, use a Vector to wrap the flag.
wobelisk能否稍微详细的解释一下第3点,
用序列化输出和直接往文件中写TRUE有什么不同吗?
避免同步代码是什么意思?
另外如果处理终了修改文件内容失败了又如何处理?谢谢.
由于正在干的项目是用solaris ,所以我采用直接执行solaris命令来输出进程的方法,然后再得到标准输出,从标准输出中判断是否有两个相同的进程。solaris 的系统命令用"ps -all -f"。由于这个项目规定必须用jdk1.3,所以这是没有办法的办法,如果OS是windows的话可以用win32 api来取得进程名。
如果可以用jdk1.4的话,还是用锁定文件的方式来判断得好。在这里谢谢各位的解答了。
You may want to prevent the user from spawning multiple copies of your Java application. Here are two approaches:
1:Test for the presence of a busy file. If one exists, abort. If not create one. The test and create can be in the bat file that triggers the app or in the app itself. When the app exists it deletes the busy er file. The main problem with this simple approach is if your app crashes, it won't delete the busy file. You have to manually delete it before you can run the app again.
2:Another approach is to have your application open a ServerSocket on a particular port number. The OS will prevent other processes from opening a ServerSocket that uses the same port. If you start the application and are unable to open your ServerSocket (i.e. if you get an "address already in use" exception) assume that the application is already running. In that case, you can use a Socket to connect to the running application and pass it whatever commands you like, or just abort.
forjie(我爱我家) (
please think it over then give a right answer.ok????
但这样就牺牲了java跨平台的特性。
也可以先获取系统平台,然后根据不同的平台做不同的处理,但总感觉不是什么好注意。
还是建议用我上面推荐的1:用文件2:用端口号