4

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();
}
gnat
  • 6,213
  • 108
  • 53
  • 73
divz
  • 7,847
  • 23
  • 55
  • 78
  • i dont know where the question and the code converge, but you can use synchronized blocks where ever you want to prevent a multi threaded havoc :-) – aishwarya Dec 22 '11 at 07:58

2 Answers2

7

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.

andrey
  • 842
  • 4
  • 6
  • Thanks Aaron! That way it looks better. – andrey Dec 22 '11 at 08:09
  • 1
    can i write this synchronized(lock) { .... } inside doPost .i writed this way but still it not working public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { synchronized(lock) { RootSipResourceApp.updateRootFile(strDirectorypath, strappID, appNames); System.out.println("after................"); }} – divz Dec 22 '11 at 08:30
  • yes, you can put synchronized(lock) { .... } on any code block {..}. More decriptive about it here: http://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html – andrey Dec 22 '11 at 11:14
3

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

public synchronized static void updateRootFile
dku.rajkumar
  • 18,414
  • 7
  • 41
  • 58