synchronized needed or not for this service call involves file i/o

Viewed 50

In Spring boot , a method in service layer work as follows ,For each machine's model id ,Need to create a file on the Fly with name machine_info.txt and Pass it to controller. Below is the code tried

My doubt is that should i make this method synchronized or not ? ,because there is a possibility of writing same file path by multi thread in the system.
In order to test this out .I created Jmeter test to hit three different machine Ids simultaneously with 10 Thread and 5 iteration.
I thought service should break because of simultaneous access But it didn't break and 150 request completed successfully.
I end up in confusion as it didn't break the code, should i use synchronized method or not for this situation .Explain if I'm missing something.

  public File downloadMachineReports(Long machineId) throws IOException {
        Machine machine = machineRepository.getOne(machineId);// Getting Machine Info
    //Adding Machine metadata File
            File infoFile = new File("machine_info.txt"); // Thinking this line will break becuase of Multithreading
            List<String> lines = new ArrayList<String>();// As it lead to same path of filesystem
            lines.add("Machine :" + machine.getname());
            lines.add("Mac ID :" + machine.getMacId());
            Files.write(infoFile.toPath(), lines);//Writing to same file path 
        return infoFile;
    }

enter image description here

0 Answers
Related