java为了跨平台,没有可以与操作系统进程打交道的api,所以要想限制多个应用程序打开,就得占用操作系统的某个特定资源,这个资源要符合这样的条件:就算程序不正常退出,也能自动被释放。符合这个条件的东西,或者说以我浅薄的java水平能操作的资源,就只有文件和端口了。下面是用端口作“山头”写的一个类: 
java 代码
  1. protected boolean running;   
  2.   
  3.     protected ServerSocket ss;   
  4.   
  5.     protected int range;   
  6.   
  7.     public ProgrameLock(int range)   
  8.     {   
  9.     this.range = range;   
  10.     }   
  11.   
  12.     public boolean checklock(int port)   
  13.     {   
  14.     Socket s = new Socket();   
  15.     for (int i = 0; i <= range; i++)   
  16.     {   
  17.         try  
  18.         {   
  19.         s.connect(new InetSocketAddress("127.0.0.1", port + i));   
  20.         int len = s.getInputStream().available();   
  21.         if (len > 0)   
  22.         {   
  23.             byte tb[] = new byte[len];   
  24.             s.getInputStream().read(tb);   
  25.             String ts = new String(tb);   
  26.             if (ts.equals("Iamhere"))   
  27.             {   
  28.             return true;   
  29.             }   
  30.         }   
  31.         }   
  32.         catch (UnknownHostException e)   
  33.         {   
  34.         continue;   
  35.         }   
  36.         catch (IOException e)   
  37.         {   
  38.         continue;   
  39.         }   
  40.     }   
  41.     return false;   
  42.     }   
  43.   
  44.     public void lock(int port)   
  45.     {   
  46.     OutputStream tos;   
  47.     try  
  48.     {   
  49.         ss = new ServerSocket(port);   
  50.         running = true;   
  51.         while (running)   
  52.         {   
  53.         try  
  54.         {   
  55.             Socket ts = ss.accept();   
  56.             tos = ts.getOutputStream();   
  57.             tos.write("Iamhere".getBytes());   
  58.             tos.flush();   
  59.             tos.close();   
  60.         }   
  61.         catch (IOException e)   
  62.         {   
  63.         }   
  64.         }   
  65.     }   
  66.     catch (IOException e)   
  67.     {   
  68.         if (port >= range)   
  69.         {   
  70.         return;   
  71.         }   
  72.         lock(port + 1);   
  73.     }   
  74.     }   
  75.   
  76.     public void unlock()   
  77.     {   
  78.     running = false;   
  79.     try  
  80.     {   
  81.         ss.close();   
  82.     }   
  83.     catch (IOException e)   
  84.     {   
  85.     }   
  86.     }  
用法: 
java 代码
  1. final plock = new ProgrameLock(5);  

新开一个线程,用lock方法占领桥头堡:

  1. new Thread(new Runnable()   
  2.     {   
  3.         public void run()   
  4.         {   
  5.         if (plock.checklock(5000))   //如果桥头堡已经被占领,就只好灰溜溜地退了
  6.         {   
  7.             System.exit(0);   
  8.         }   
  9.         else  
  10.         {   
  11.             plock.lock(5000);   
  12.         }   
  13.         }   
  14.     }).start();  

 

再来来一个使用文件锁的:

 

java 代码
  1. package src;   
  2.   
  3. import java.io.File;   
  4. import java.io.FileNotFoundException;   
  5. import java.io.IOException;   
  6. import java.io.RandomAccessFile;   
  7. import java.net.ServerSocket;   
  8. import java.nio.channels.FileChannel;   
  9. import java.nio.channels.FileLock;   
  10. import java.nio.channels.OverlappingFileLockException;   
  11.   
  12. public class FileProgrameLock   
  13. {   
  14.   
  15.     protected boolean running;   
  16.   
  17.     protected ServerSocket ss;   
  18.   
  19.     protected FileLock lock;   
  20.   
  21.     public boolean checklock(String fileName) throws FileNotFoundException   
  22.     {   
  23.     File tf = new File(fileName);   
  24.     FileChannel channel = new RandomAccessFile(tf, "rw").getChannel();   
  25.     try  
  26.     {   
  27.         FileLock tl = channel.tryLock();   
  28.         if (tl == null)   
  29.         {   
  30.         return true;   
  31.         }   
  32.         else  
  33.         {   
  34.         return false;   
  35.         }   
  36.     }   
  37.     catch (OverlappingFileLockException e)   
  38.     {   
  39.         return true;   
  40.     }   
  41.     catch (IOException e)   
  42.     {   
  43.         return true;   
  44.     }   
  45.     }   
  46.   
  47.     public boolean lock(String fileName) throws IOException   
  48.     {   
  49.     File tf = new File(fileName);   
  50.     FileChannel channel = new RandomAccessFile(tf, "rw").getChannel();   
  51.     try  
  52.     {   
  53.         lock = channel.lock();   
  54.         return true;   
  55.     }   
  56.     catch (OverlappingFileLockException e)   
  57.     {   
  58.         return false;   
  59.     }   
  60.     }   
  61.   
  62.     public void unlock()   
  63.     {   
  64.     try  
  65.     {   
  66.         lock.release();   
  67.     }   
  68.     catch (IOException e)   
  69.     {   
  70.     }   
  71.     }   
  72.   
  73.  }   

 

欢迎大家抛砖,我顺便可以引引玉

评论
liangguanhui 2007-02-10
之前有一篇文章就说说这个问题的,我觉得socket端口这个方法比较好。
发表评论

您还没有登录,请登录后发表评论

aninfeel
搜索本博客
博客分类
最近加入圈子
最新评论
评论排行榜