1

I have launched my program that have two loop like this :

for(int i=0;i<100;i++)
    String[] articles = getArticles(i);
    for(int j=0;j<articles.length;j++)
        process(articles[j]);

Can I modify the running program so it stop at i = 1? If how to do that?

The program will take days to finish and I want to stop but I must know where it was stoped so the next time I can resume it.

3
  • You do not need to modify the code when it is running to do what you have stated. You can just add logic to your program, or run in a debugger. The latter is probably a better option if you are testing. Commented Aug 1, 2014 at 23:06
  • I think you haven't understood the question. I have already run the program that why I want to modify it while it's running. the answer by @tucuxi is really interesting! Commented Aug 2, 2014 at 10:01
  • 1
    I see your point. I am not sure about actually modifying the code, but you can perform some realtime debugging tasks using visualvm. Even if it does not help you in this case, I would recommend looking it up. Commented Aug 4, 2014 at 20:09

5 Answers 5

3

Unfortunately you won't be able to stop a running program at a predictable spot unless debugger is on.

Stop your program, change the code, and start it up again.

Sign up to request clarification or add additional context in comments.

Comments

2

It is possible (lots of things are). But it can be very, very hard - unless you are skilled at looking at Java bytecode and know your OS internals, I would not try.

Assuming you are running under linux, you can suspend and restart processes via kill -STOP <pid> and kill -CONT <pid>. You can also take a stack-trace of a running java process via jstack, and look at the running threads and decompiled code.

Comments

0

You could do this

static boolean stopLoop = false;

static int[] runLoop(int i, int j) {
  for(;i<100;i++) {
    if(stopLoop) return new int[i, j];
    String[] articles = getArticles(i);
    for(;j<articles.length;j++) {
        process(articles[i]);
    }
    j = 0;
  }
}

Comments

0

(You probably wanted to write process(articles[j]); in your inner loop.)

You can save last value of i and j into a file after processing of each article, then you can terminate your program anytime you want, and next time just read the values of i and j from your file and you don't have to process them again.

Or set 100 article processes per program run, or process for 20 minutes, there are so many possibilities.

Comments

0

There is no straightforward way to do this, but with a little extra legwork it can be done. One way is to output the latest successful iteration to persistent storage (like a file), then read the file if it exists to find out where to start. The example below shows one way to do it:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

public class Test
{
    public static void main(String[] args)
    {
        int start;

        // Try to get the last successful run
        File file = new File(System.getenv("HOME"), ".myprog");
        if(file.exists()) {
            try(BufferedReader reader = new BufferedReader(new FileReader(file))) {
                String line = reader.readLine();
                if(line == null) {
                    throw new NumberFormatException("Empty file, no number");
                }
                start = Integer.parseInt(line);
            } catch(IOException | NumberFormatException ex) {
                System.err.println("Unable to read progress: " + ex.getMessage());
                System.err.println("Starting from the beginning");
                start = 0;
            }
        } else {
            start = 0;
        }

        // ... Your declarations go here

        for(int i = start; i < 100; i++) {
            String[] articles = getArticles(i);
            for(int j=0;j<articles.length;j++) {
                process(articles[i]);
            }

            // Record last successful run (this reopens the file every time)
            try(PrintWriter writer = new PrintWriter(file)) {
                writer.println(i);
            } catch(IOException ex) {
                System.err.println("Unable to record progress: " + ex.getMessage());
            }
        }
    }
}

Note that I run on a Linux machine, so I called my file ~/.myprog. You may want to change that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.