当前位置: 技术问答>java相关
请问:怎么删除系统中的唯一的实例???
来源: 互联网 发布时间:2014-12-30
本文导语: 我有一个类 public class OnlyInstance { private static OnlyInstance instance = new OnlyInstance(); private OnlyInstance() { super(); ... ... ...; } public static OnlyInstance getOnlyInstance() { return instance; } } 这样做可以使在系统...
我有一个类
public class OnlyInstance {
private static OnlyInstance instance = new OnlyInstance();
private OnlyInstance() {
super();
... ... ...;
}
public static OnlyInstance getOnlyInstance() {
return instance;
}
}
这样做可以使在系统中始终只有一个这个类的实例存在,
OnlyInstance oi = OnlyInstance.getOnlyInstance();
但是当我想从系统中删除这个实例,请问应该怎么做???
oi = null;
好象只能使 oi 这个引用为空,实际上这个实例依然存在.
请各位高手赐教,谢谢!!!
public class OnlyInstance {
private static OnlyInstance instance = new OnlyInstance();
private OnlyInstance() {
super();
... ... ...;
}
public static OnlyInstance getOnlyInstance() {
return instance;
}
}
这样做可以使在系统中始终只有一个这个类的实例存在,
OnlyInstance oi = OnlyInstance.getOnlyInstance();
但是当我想从系统中删除这个实例,请问应该怎么做???
oi = null;
好象只能使 oi 这个引用为空,实际上这个实例依然存在.
请各位高手赐教,谢谢!!!
|
import java.io.*;
import java.net.*;
public class JustOne {
SimpleDummyServer sds = null;
public static void main(String args[]){
new JustOne().doit();
}
public void doit() {
try {
Socket clientSocket = new Socket("localhost", SimpleDummyServer.port);
System.out.println("*** Already running!");
System.out.println("Program Exit!");
System.exit(1);
}
catch (Exception e) {
e.printStackTrace();
sds = new SimpleDummyServer();
sds.start();
}
while(true) {
try { System.out.print("runing..."); Thread.sleep(1000); }
catch(Exception e) { e.printStackTrace(); }
}
}
}
================================================================
import java.io.*;
import java.net.*;
public class SimpleDummyServer extends Thread {
// you may need to customize this for your machine
public static final int port = 80 ;
ServerSocket serverSocket = null;
Socket clientSocket = null;
public void run() {
try {
// Create the server socket
serverSocket = new ServerSocket(port, 1);
while (true) {
// Wait for a connection
clientSocket = serverSocket.accept();
System.out.println("*** Got a connection! ");
clientSocket.close();
}
}
catch (IOException ioe) {
System.out.println("Error in SimpleDummyServer: " + ioe);
}
}
}
import java.net.*;
public class JustOne {
SimpleDummyServer sds = null;
public static void main(String args[]){
new JustOne().doit();
}
public void doit() {
try {
Socket clientSocket = new Socket("localhost", SimpleDummyServer.port);
System.out.println("*** Already running!");
System.out.println("Program Exit!");
System.exit(1);
}
catch (Exception e) {
e.printStackTrace();
sds = new SimpleDummyServer();
sds.start();
}
while(true) {
try { System.out.print("runing..."); Thread.sleep(1000); }
catch(Exception e) { e.printStackTrace(); }
}
}
}
================================================================
import java.io.*;
import java.net.*;
public class SimpleDummyServer extends Thread {
// you may need to customize this for your machine
public static final int port = 80 ;
ServerSocket serverSocket = null;
Socket clientSocket = null;
public void run() {
try {
// Create the server socket
serverSocket = new ServerSocket(port, 1);
while (true) {
// Wait for a connection
clientSocket = serverSocket.accept();
System.out.println("*** Got a connection! ");
clientSocket.close();
}
}
catch (IOException ioe) {
System.out.println("Error in SimpleDummyServer: " + ioe);
}
}
}