加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-13 20:06:30 所属栏目:百科 来源:网络整理
导读:public class MainApp { boolean packFrame = false; /** * Construct and show the application. */ public MainApp() { // 控制程序只能开启一次 // SingleThread single = new SingleThread(); // single.start(); if (instanceControl()) { System.out.p

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; } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读