Package ru.jnano.swing

Source Code of ru.jnano.swing.AppLock

package ru.jnano.swing;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

import javax.swing.JOptionPane;

public class AppLock {
 
  public static boolean singletonApp(String namefile) throws IOException{
    final File f = new File(namefile);
    if (f.exists()) f.delete();
    final FileChannel channel = new RandomAccessFile(f, "rw").getChannel();
    final FileLock lock = channel.tryLock();
    if(lock == null){
      JOptionPane.showMessageDialog(null, "���������� ��� ��������");
      channel.close();
      return false;
    }
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
      @Override
      public void run() {
        if(lock != null){
          try {
            lock.release();
            channel.close();
            f.delete();
          } catch (IOException e) {
            e.printStackTrace();
          }
         
        }
      }
    }));
    return true;
  }
 
}
TOP

Related Classes of ru.jnano.swing.AppLock

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.