0

I'm using scanner to read lines from file and replace some text in each line based on given pattern. This is done inside an API. My program is a multithreaded one. At once, more than one thread can call this particular API.

Following is the line scanner initialization line where the thread crashes:

public static void replaceInFile(Properties replacements, Path targetFile) {
    ...
    Scanner in = new Scanner(targetFile, "UTF-8");
    ...
}

I'm sure no single file will be accessed by two threads at once. Can anyone hint me in the right direction as to what is happening?

UPDATE:

public Void call() throws Exception {
        Iterator it = paths.iterator();
        while(it.hasNext()){
          try {
            String filePath = it.next().toString();
            //BuildUtil replacer = new BuildUtil();
            BuildUtil.replaceInFile(replacements, Paths.get(filePath));
          } catch(Exception e) {
             e.printStackTrace();
          }
}

This is the call() of the thread. Now I observe that it shows "Frame not available" even before stepping into the BuildUtils's replaceInFile method and sometimes after entering in there..I'm not able to figure out what's wrong.. The main thread is exiting I think but I see nothing strange happening here which should make it exit unexpectedly.

A.R.K.S
  • 1,692
  • 5
  • 18
  • 40
  • Assuming that by "crashes" you mean throws exception, can you include the exception details – Misha Feb 18 '16 at 01:27
  • I'm sorry, I meant the program just exits. My debugger says something on the lines of "frame not available" ... – A.R.K.S Feb 18 '16 at 01:28
  • 3
    I think you need to share more code here. Anything based on above two line will be a guess... – Peeyush Feb 18 '16 at 01:42
  • I shall update more soon. Meanwhile, when I step through an API that is defined in a different package, it steps me into .class file. Any way to get into the actual source file for debugging? I'm using intelliJ editor – A.R.K.S Feb 18 '16 at 01:42
  • see this answer on how to attach sources in idea http://stackoverflow.com/questions/2485173/how-to-step-into-code-from-jars-non-jdk-using-intellij – Ammar Feb 18 '16 at 04:49
  • 1
    If the entire application exits, what makes you so confident that this thread, executing the posted line, is the cause? – Holger Feb 18 '16 at 09:11
  • I'm not sure anymore. I stepped into the thread execution and I get "Frame not available" once the execution enters around this place. I'm not able to figure out what is happening.I'm only sure that the thread is not achieving its goal. This was the reason to step into thread execution. – A.R.K.S Feb 18 '16 at 16:05
  • But I'm not sure now because it doesn't even happen at the same point anymore. Sometimes it says "Frame not available" just when I click "step in" to the API. Any ideas what could be wrong? – A.R.K.S Feb 18 '16 at 16:19

1 Answers1

0

I found it. Actually it was my stupidity. I forgot to wait for the threads to exit and so the main thread exited even before threads could complete. Sorry for bothering!

So now I do :

for (int i = 0; i < threadsUsed; i++) {
  pool.take().get();
}

for all the threads and shutdown the executor service in finally block

A.R.K.S
  • 1,692
  • 5
  • 18
  • 40