0

I have a multi-threaded compiler developed in Java. It receives n filepaths and starts one thread for each filepath received.

Everything works fine if I do multiple executions with a single file. However, when I input multiple files and execute it one time per second it shows different parsing outputs in the source-code loaded.

It was implemented using the recursive descent parser technique. The function that calls the threads is shown as follows:

public static void main(String[] args) {

    if(args.length == 0){
        System.out.println("No file path informed. Please inform file paths.");
        System.out.println("Usage: java -jar KPiler.jar file1.txt file2.txt");
    }

    List<String> paths = new ArrayList<>();
    List<String> errorMessages = new ArrayList<>();
    List<Parser> compileJobs = new ArrayList<>();

    paths.addAll(Arrays.asList(args));


    while(paths.size() > 0 || compileJobs.size() > 0){

        if(compileJobs.size() < MAX_JOBS && paths.size() > 0){
            compileJobs.add(new Parser(paths.remove(0)));
            compileJobs.get(compileJobs.size() -1).start();
        }

        for(int i=0 ; i<compileJobs.size(); i++){
            if(!compileJobs.get(i).isAlive()){
                errorMessages.add(compileJobs.get(i).getErrorMessages());
                compileJobs.remove(i);
            }
        }
    }
    //PRINTING OUTPUT
    for(int i = 0; i<errorMessages.size(); i++){
        System.out.println(errorMessages.get(i));

    }

}

Does it have to do with multiple file accesses?

  • 1
    Your compiler isn't thread-safe. – user207421 Nov 14 '17 at 04:06
  • How can I fix it? Because each Parser object does not interact with the others, so I thought it was not necessary to synchronize the whole thing. – Josué Rocha Nov 14 '17 at 09:00
  • 1
    Please post the code of Parser. The problem is most likely there. – TwoThe Nov 14 '17 at 13:39
  • I figured out the problem. I had a shared object corresponding to the SymbolTable class. I had not synchronized its methods, so a previously read identifier was not being stored into the SymbolTable class in the right time, consequently outputting errors upon consulting the identifier's info from the SymbolTable class. I am so sorry to bother for such kind of mistake. – Josué Rocha Nov 14 '17 at 15:20

0 Answers0