1
import java.lang.Thread;
import java.util.Scanner;

class Running extends Thread{

    private boolean Run=true;

    public void shutdown(){
        Run=false;
    }
    public void run(){
        while(Run){
            for(int i=0;i<1000;i++){
                System.out.println("Starting Counter:"+i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

public class Volatile {
    public static void main(String[] args) {
        Running run1=new Running();
        run1.start();
        System.out.println("Press any key to stop thread execution");
        Scanner sc=new Scanner(System.in);
        sc.nextLine();
        run1.shutdown();
    }
}

I am using a volatile key word to stop the thread execution. But couldn't get the solution

Gray
  • 115,027
  • 24
  • 293
  • 354
Java05461
  • 31
  • 3
  • 2
    I don't se the "volatile keyword" in your code. – Gray Jan 16 '14 at 17:39
  • as long as the condition is true it will run, make condition to something that is out of the loop condition – Saqib Jan 16 '14 at 17:39
  • As a side issue, pressing a key will not stop the execution of your thread. You would need to press enter after whatever you type into the console. – Jems Jan 16 '14 at 17:41

5 Answers5

4

What should I be doing to stop the thread execution?

So your post mentions the volatile keyword but I don't see it in the code you've posted. You need to make sure that Run is a volatile boolean so that it can be changed in the main thread and the other thread will see the changes.

private volatile boolean Run=true;

But I think the problem is that you are testing for this Run only in the outer while loop. Maybe you want it also in the inner counting loop. Something like:

for(int i = 0; i < 1000 && Run; i++) {

Right now, once the loop start counting it will have to finish before the Run boolean is checked. That may be 1000 seconds in the future.

Gray
  • 115,027
  • 24
  • 293
  • 354
1

You are using the right pattern, but apply it a bit wrong:

Your Runflag will only be checked after 1000 seconds, because it will only be checked after the for loop finished. Change your code to the following:

public void run(){
    while(Run){
    for(int i=0;(i<1000) && (Run == true);i++){

        System.out.println("Starting Counter:"+i);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
    }
}

Please note you will have to completely remove the while loop if you don't want your for loop to be executed endlessly effectively.

FD_
  • 12,947
  • 4
  • 35
  • 62
0

A time ago the thread was stopped simply by calling the Thread's stop() function. Nowadays, it's deprecated because it's unsecure, so you have to stop the Thread from the inside of the Thread. You'll find a good example here. Keep in mind that doing this doesn't stop immediately the thread, it just tells it's available for stopping and Android will stop it whenever it finds it necessary.

Community
  • 1
  • 1
nKn
  • 13,691
  • 9
  • 45
  • 62
0

Your code stays in the for-loop for 1000 seconds until it finally reaches the while(run) branch again. I guess this is why its not working properly.

Yannic Welle
  • 207
  • 2
  • 10
-1

These is the sample .You can create a boolean field and check it inside run:

public class Sample implements Runnable {

   private volatile boolean isRunning = true;

   public void run() {

     while (isRunning) {
         //do work
     }
   }

   public void kill() {
       isRunning = false;
   }

}

To stop it just call

sample.kill();

This should work.

vinay Maneti
  • 1,447
  • 1
  • 23
  • 31