0
votes

In the following code why is it that calling the shutDown() method using a new instance doesn't stop the while loop but when I call the shutDown() method using the existing instance of Processor class then the loop terminates?

It is my understanding ( from this link difference between processes and threads) that threads are subsets of processes and all threads within a process share the same heap, memory etc. Now if the 'private boolean running' variable is shared between all threads(since it is a class variable) then why doesn't the new Processor instance new Processor().shutDown(), change said variable to false?

import java.util.Scanner;

class Processor extends Thread {
    private  boolean running = true;

    public void run() {
        while(running) {
            System.out.println(" hello ppl");
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void shutDown() {
        running = false;

    }
}


public class App1 {
    public static void main(String[] args) {
        Processor p1 = new Processor();
        p1.start();

        System.out.println("press enter to stop");
        Scanner scanner = new Scanner(System.in);
        scanner.nextLine();


        p1.shutDown(); // THIS WORKS
        //new Processor().shutDown(); // THIS DOESNT!!


    }

}
3
Your error lies with the private boolean running variable – it is a class instance field (each instance of Processor has its own) and not a class field (all instance share the same one). To use a class field as you intended, you have to declare it static. However, you must synchronize the access to the shared space. - interDist

3 Answers

1
votes

You tries to change instances variable. Every instance have its own set of variables. In your case you should use class variables with modificator static - then all your threads will down after .shotDown() call.

PS please learn Java before trying to use it. It is basics of language and programming using this language.

0
votes
new Processor().shutdown(); 

wont work because where the threat is started?

For a thread to run, first of all, it need to be started. The above code is creating a new thread, but doesn't start it!

Modify it like,

Processor p = new Processor();
p.start();
p.shutdown();

Try like above.. It will work.

In case you want the same thread to be stopped, assign the boolean running globally, or make it static like the following..

import java.util.Scanner;

class Processor extends Thread {
    private static boolean running = true;

    public void run() {
        while(running) {
            System.out.println(" hello ppl");
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void shutDown() {
        running = false;

    }
}


public class App1 {
    public static void main(String[] args) {
        Processor p1 = new Processor();
        p1.start();

        System.out.println("press enter to stop");
        Scanner scanner = new Scanner(System.in);
        scanner.nextLine();


        //p1.shutDown(); // THIS WORKS
        new Processor().shutDown(); // THIS WILL WORK NOW!!


    }

But this will stop all the instances of the running thread. Good practice is the former! Your solution is the latter!

0
votes

When you create a new instance of the class with new Processor(), it gets its own "space" for variables. That has nothing to do with Threads, its just object orientated. Every instance of a class has its own variables.