title: Reading and writing in files with Thread and semaphore
body: <p>I'm new in semaphore and i have any questions. I have a thread which start and read lines from text file A and write them in to other text file B. I wrote this code, but  I'm not sure whether thread block critical section and sync properly. Because and others threads can operation with these files. </p>
  
  <pre><code>public static void main(String[] args)  {
  
              Thread thread = new Thread(new ThreadManager());
              thread.start();
  
        }
  </code></pre>
  
  <p>Thread class:</p>
  
  <pre><code>public class ThreadManager extends Thread {
        private Semaphore semaphore;
  
        public ThreadManager() {
              this.semaphore = new Semaphore(1);
        }
  
        public void run() {
  
              try {
                    this.semaphore.acquire();
  
                    BufferedReader br = null;
                    String line;
                    String fileNme = "threadLog.txt";
                    ArrayList&lt;String&gt; fileLines = new ArrayList&lt;String&gt;();
                    int numLine = 0;
  
                    File outFile = new File("$$$$$$$$.tmp");
  
                    // input
                    FileInputStream fis = null;
                    PrintWriter out = null;
                    try {
                          fis = new FileInputStream(fileNme);
  
                          // output
                          FileOutputStream fos = new FileOutputStream(outFile);
                          out = new PrintWriter(fos);
                    } catch (FileNotFoundException e2) {
                          // TODO Auto-generated catch block
                          e2.printStackTrace();
                    }
                    BufferedReader in = new BufferedReader(new InputStreamReader(fis));
  
                    try {
                          while ((line = in.readLine()) != null) {
                                fileLines.add(line);
                          }
                    } catch (IOException e1) {
                          // TODO Auto-generated catch block
                          e1.printStackTrace();
                    }
  
                    if (!fileLines.isEmpty()) {
                          int middleLine = (int) Math.round(fileLines.size() / 2);
                          fileLines.add(middleLine, Thread.currentThread().getName());
  
                          for (int i = 0; i &lt; fileLines.size(); i++) {
                                out.println(fileLines.get(i));
                          }
  
                          out.flush();
                          out.close();
                          try {
                                in.close();
                                new File(fileNme).delete();
                                outFile.renameTo(new File(fileNme));
                          } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                          }
  
                    }
                     this.semaphore.release();
              } catch (InterruptedException e3) {
                    // TODO Auto-generated catch block
                    e3.printStackTrace();
              }
  
        }
  </code></pre>