如何防止静态方法同时被多个Servlet线程访问

发布于 2024-12-22 16:37:31 字数 2307 浏览 1 评论 0原文

这是我使用的代码
删除和重命名目录中的大量文件。

protected void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
    response.setContentType("text/html; charset=UTF-8");
    response.setCharacterEncoding("UTF-8");
    RootSipResourceApp.updateRootFile(strDirectorypath, strappID, appNames);
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
    dispatcher.forward(request, response);
}

public static void updateRootFile(String directorypath, String appID, String[] appName) 
  throws IOException {
    try {
        FileInputStream fin = null;
        File[] listOfFiles=fileLists(directorypath);
        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                rootFiles = listOfFiles[i].getName();
                if (rootFiles.endsWith(".properties") || rootFiles.endsWith(".PROPERTIES")) {
                    fin = new FileInputStream(directorypath + rootFiles);
                    properties.load(new InputStreamReader(fin, Charset.forName("UTF-8")));
                    String getAppName = properties.getProperty("root.label." + appID);
                    String propertyStr = "root.label." + appID;
                    saveFile(fin, getAppName, directorypath + rootFiles, propertyStr, appName[i]);
                }
            }
        }
    } catch (Exception e) {
        System.out.println("expn-" + e);
    }
}

public static void saveFile(FileInputStream fins, String oldAppName, String filePath, String propertyStr, String appName)
  throws IOException {
    String oldChar = propertyStr + "=" + oldAppName;
    String newChar = propertyStr + "=" + appName;
    String strLine;
    File f1 = new File(filePath);
    File f2 = new File("C:\\Equinox\\RootSipResource\\root\\root_created.properties");
    BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream(f1), "UTF-8"));
    OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(f2), "UTF-8");
    while ((strLine = br.readLine()) != null) {
        strLine = strLine.replace(oldChar, newChar);
        out.write(strLine);
        out.write("\r\n");
    }
    out.flush();
    out.close();
    br.close();
    fins.close();
}

here is the code I used for
deleting and renaming lots of file inside a directory.

protected void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
    response.setContentType("text/html; charset=UTF-8");
    response.setCharacterEncoding("UTF-8");
    RootSipResourceApp.updateRootFile(strDirectorypath, strappID, appNames);
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
    dispatcher.forward(request, response);
}

public static void updateRootFile(String directorypath, String appID, String[] appName) 
  throws IOException {
    try {
        FileInputStream fin = null;
        File[] listOfFiles=fileLists(directorypath);
        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                rootFiles = listOfFiles[i].getName();
                if (rootFiles.endsWith(".properties") || rootFiles.endsWith(".PROPERTIES")) {
                    fin = new FileInputStream(directorypath + rootFiles);
                    properties.load(new InputStreamReader(fin, Charset.forName("UTF-8")));
                    String getAppName = properties.getProperty("root.label." + appID);
                    String propertyStr = "root.label." + appID;
                    saveFile(fin, getAppName, directorypath + rootFiles, propertyStr, appName[i]);
                }
            }
        }
    } catch (Exception e) {
        System.out.println("expn-" + e);
    }
}

public static void saveFile(FileInputStream fins, String oldAppName, String filePath, String propertyStr, String appName)
  throws IOException {
    String oldChar = propertyStr + "=" + oldAppName;
    String newChar = propertyStr + "=" + appName;
    String strLine;
    File f1 = new File(filePath);
    File f2 = new File("C:\\Equinox\\RootSipResource\\root\\root_created.properties");
    BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream(f1), "UTF-8"));
    OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(f2), "UTF-8");
    while ((strLine = br.readLine()) != null) {
        strLine = strLine.replace(oldChar, newChar);
        out.write(strLine);
        out.write("\r\n");
    }
    out.flush();
    out.close();
    br.close();
    fins.close();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

古镇旧梦 2024-12-29 16:37:31

选项 1:

public static void updateRootFile 更改为 public static synchronized void updateRootFile

选项 2:

向 servlet 类添加成员

private final static Object lock = new Object();

,然后将 updateRootFile 方法中的代码包装到

synchronized(lock) {
   ....
}

不同之处在于选项 1 锁定整个 servlet 类(其所有同步方法),而选项 2 允许从其他线程调用其他 servlet 的方法,除了 updateRootFile()

我相信这是最权威的指南之一在这里。第 07 - 11 章与多线程代码相关。

Option 1:

change public static void updateRootFile to public static synchronized void updateRootFile

Option 2:

Add member to servlet class

private final static Object lock = new Object();

then wrap code inside updateRootFile method into

synchronized(lock) {
   ....
}

The difference is that Option 1 locks whole servlet class (all its synchronized methods) while Option 2 allow calling other servlet's methods from other threads except updateRootFile()

I believe one of the most definitive guides is here. Chapters 07 - 11 related to multithreaded code.

一杆小烟枪 2024-12-29 16:37:31

您可以为此使用synchronized 方法。您可以在此处阅读相关内容

public synchronized static void updateRootFile

you can use synchronized method for this. you can read about it here

public synchronized static void updateRootFile
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文