0

Program A:

    public void startThreadPool2(boolean flag) {
    while(flag) {
        Runnable run = new Runnable() {
            @Override
            public void run() {
                System.out.println("running");
            }
        };
        service.execute(run);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Program B:

    public static void main(String[] args) {
    SentimentAnalyse sentimentAnalyse = SentimentAnalyse.getInstance();
    sentimentAnalyse.startThreadPool2(true);
}

Program C:

public static void main(String[] args) {
    //how to stop the while loop
}

A while loop keep running and I don't know how to stop it in another program.

//update Your ways maybe all works, I find my singleton don't work due to eclipse use different class loader in different program.

6
  • The flag is passed by-value so the loop will run forever or not at all. In addition, while (flag) { if (flag == false) break; ... } is redundant. Commented Dec 2, 2014 at 21:05
  • It is not clear what loop you refer to in a Program C. Commented Dec 2, 2014 at 21:06
  • Stop while loop in A called by program B. I don't know how to explain these exactly. Maybe we can say stop program B or stop the while loop in program A. Commented Dec 2, 2014 at 21:08
  • The flag doesn't matter. You can think it is while(true). Commented Dec 2, 2014 at 21:09
  • Is this loop executed in the same thread as the main method of a program C or not? Commented Dec 2, 2014 at 21:09

1 Answer 1

1

Store the flag in the SentimentAnalyse and provide a setter for it! In porgram c (what ever this is) you need a reference to the instance of SentimentAnalyse, that you've created in program b. Stop the loop in programm a by calling its setter with false from program c

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

1 Comment

It seems don't work. I can find same instance by using Singleton. In program B, it have call startThreadPool2() with while(true). If I set the flag in C, that means calling startThreadPool2() with while(false), and nothing happen. We can find same instance, but we can't find the method.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.