public class Singleton { private Singleton sing = new Singletion(); private Singleton(){ } public static synchronized Singleton getInstance(){ return sing; } }
上面改一下。 private static Singleton sing = new Singletion();
public class Singleton { private Singleton sing = new Singletion(); private Singleton(){ } public static synchronized Singleton getInstance(){ return sing; } }
boolean isApplicationRunning(final InetAddress host, final int port) { try { Socket socket = new Socket(host, port); try { socket.setKeepAlive(true); String test = "" + System.currentTimeMillis(); OutputStream out = socket.getOutputStream(); InputStream in = null; try { System.out.println("found running control service on " + host + ":" + port); out.write(("PING " + test).getBytes()); out.flush(); socket.shutdownOutput(); in = socket.getInputStream(); StringBuffer commandResult = new StringBuffer(); byte[] buf = new byte[16]; int len; while ((len = in.read(buf)) != -1) { commandResult.append(new String(buf, 0, len)); } socket.shutdownInput(); if (commandResult.toString().startsWith("OK") && (commandResult.toString().indexOf(test) != -1)) { System.out.println("PING command succeed"); return true; } System.out.println("PING command failed"); } finally { try { out.close(); } catch (IOException ioe) { // ignore } if (in != null) { try { in.close(); } catch (IOException ioe) { // ignore } } } } finally { socket.close(); } } catch (IOException ioe) { System.out.println( "seems that there is no control service running on " + host + ":" + port); //ioe.printStackTrace(); } return false; }
我觉得可以用tryLock()一个文件啊,用端口的话要是其程序也用那个就没折了
单例模式public class Singleton { private static Singleton sing = new Singletion(); private Singleton(){ } public static synchronized Singleton getInstance(){ return sing; } }
public class Singleton {
private Singleton sing = new Singletion();
private Singleton(){
}
public static synchronized Singleton getInstance(){
return sing;
}
}
private static Singleton sing = new Singletion();
不考虑得太复杂了
难道楼上的还有高招?
在一个JVM下作控制的话,单例模式即可.
public final class SingleApp {
static {
System.loadLibrary("SingleApp");
} public static native boolean isRunning(String appName); //测试
public static void main(String[] args) {
System.out.println(SingleApp.isRunning("YourApplicationName"));
try {
while (true) {
Thread.sleep(10);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
头文件SingleApp.h:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class SingleApp */#ifndef _Included_SingleApp
#define _Included_SingleApp
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: SingleApp
* Method: isRunning
* Signature: (Ljava/lang/String;)Z
*/
JNIEXPORT jboolean JNICALL Java_SingleApp_isRunning
(JNIEnv *, jclass, jstring);#ifdef __cplusplus
}
#endif
#endif
Windows下CPP文件SingleApp.cpp:
#include <windows.h>
#include "SingleApp.h"JNIEXPORT jboolean JNICALL Java_SingleApp_isRunning(JNIEnv *env, jclass object, jstring appName)
{
char *AppName = (char *)env->GetStringUTFChars(appName, 0);
HANDLE hMutex = CreateMutex(NULL, FALSE, AppName);
env->ReleaseStringUTFChars(appName, AppName);
if(hMutex == NULL || GetLastError() == ERROR_ALREADY_EXISTS)
return TRUE;
return FALSE;
}
Linux/Unix下,可以用Unix Domain Socket。
简单有效的方法
启动时占用 serverSocket(XXXX);// 一个端口
如果没有把错,则此为第一个实例
否则
不转不能编译
a2w转后报错
MultiByteToWideChar没用明白
private Singleton sing = new Singletion();
private Singleton(){
}
public static synchronized Singleton getInstance(){
return sing;
}
}
final int port) {
try {
Socket socket = new Socket(host, port);
try {
socket.setKeepAlive(true);
String test = "" + System.currentTimeMillis();
OutputStream out = socket.getOutputStream();
InputStream in = null;
try {
System.out.println("found running control service on "
+ host + ":" + port);
out.write(("PING " + test).getBytes());
out.flush();
socket.shutdownOutput();
in = socket.getInputStream();
StringBuffer commandResult = new StringBuffer();
byte[] buf = new byte[16];
int len;
while ((len = in.read(buf)) != -1) {
commandResult.append(new String(buf, 0, len));
}
socket.shutdownInput();
if (commandResult.toString().startsWith("OK")
&& (commandResult.toString().indexOf(test) != -1)) {
System.out.println("PING command succeed");
return true;
}
System.out.println("PING command failed");
} finally {
try {
out.close();
} catch (IOException ioe) {
// ignore
}
if (in != null) {
try {
in.close();
} catch (IOException ioe) {
// ignore
}
}
}
} finally {
socket.close();
}
} catch (IOException ioe) {
System.out.println(
"seems that there is no control service running on "
+ host + ":" + port);
//ioe.printStackTrace();
}
return false;
}
private static Singleton sing = new Singletion();
private Singleton(){
}
public static synchronized Singleton getInstance(){
return sing;
}
}