利用文件锁,实现单一线程运行

public class MainApp { boolean packFrame = false; /** * Construct and show the application. */ public MainApp() { // 控制程序只能开启一次 // SingleThread single = new SingleThread(); // single.st

public class MainApp {
boolean packFrame = false;

/**
* Construct and show the application.
*/
public MainApp() {
// 控制程序只能开启一次
// SingleThread single = new SingleThread();
// single.start();
if (instanceControl()) {
System.out.println(“集中出单影像提取服务已经在运行中,本服务不能启动,自动退出。”);
System.exit(0);
}

MainFrame frame = new MainFrame();
// Validate frames that have preset sizes
// Pack frames that have useful preferred size info,e.g. from their
// layout
if (packFrame) {
frame.pack();
} else {
frame.validate();
}

// Center the window
frame.setLocation(GUIUtil.getLocationForWindow(new Point(),Toolkit
.getDefaultToolkit().getScreenSize(),frame.getSize()));
frame.setVisible(true);

// 服务自动启动,不需要手动点开启服务按钮或菜单。
frame.startServerAuto();
}

/**
* Application entry point.
*
* @param args
* String[]
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
// 设计不同的显示风格
UIManager.LookAndFeelInfo[] lookAndFeelInfo = UIManager
.getInstalledLookAndFeels();
UIManager.setLookAndFeel(lookAndFeelInfo[0].getClassName());
} catch (Exception exception) {
exception.printStackTrace();
}
// log4j.properties配置文件和jzcd.jar包放在同一目录下
PropertyConfigurator.configure(System.getProperty(“user.dir”)
+ “/log4j.properties”);
new MainApp();
}
});
}

public static boolean instanceControl() { boolean flag = false; try { RandomAccessFile raf = new RandomAccessFile(new File( “jzcdService.lck”),”rw”); FileChannel fc = raf.getChannel(); FileLock lock = fc.tryLock(); if (lock == null) { flag = true; } } catch (Exception ex) { ex.printStackTrace(); } return flag; } }

作者: dawei

【声明】:永州站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

为您推荐

联系我们

联系我们

0577-28828765

在线咨询: QQ交谈

邮箱: xwei067@foxmail.com

工作时间:周一至周五,9:00-17:30,节假日休息

返回顶部