Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (10.2k points)

But is it possible to print "Success" message on the condition given below in Java?

if (a==1 && a==2 && a==3) {

    System.out.println("Success");

}

Someone suggested:

int _a = 1;

int a  = 2;

int a_ = 3;

if (_a == 1 && a == 2 && a_ == 3) {

    System.out.println("Success");

}

But by doing this we are changing the actual variable. Is there any other way?

1 Answer

0 votes
by (46k points)

Yes, it's quite easy to achieve this with multiple threads, if you declare variable a as volatile.

One thread constantly changes variable a from 1 to 3, and another thread constantly tests that a == 1 && a == 2 && a == 3. It happens often enough to have a continuous stream of "Success" printed on the console.

(Note if you add an else {System.out.println("Failure");} clause, you'll see that the test fails far more often than it succeeds.)

In practice, it also works without declaring a as volatile, but only 21 times on my MacBook. Without volatile, the compiler or HotSpot is allowed to cache a or replace the if statement with if (false). Most likely, HotSpot kicks in after a while and compiles it to assembly instructions that do cache the value of a. With volatile, it keeps printing "Success" forever.

public class VolatileRace {

    private volatile int a;

    public void start() {

        new Thread(this::test).start();

        new Thread(this::change).start();

    }

    public void test() {

        while (true) {

            if (a == 1 && a == 2 && a == 3) {

                System.out.println("Success");

            }

        }

    }

    public void change() {

        while (true) {

            for (int i = 1; i < 4; i++) {

                a = i;

            }

        }

    }

    public static void main(String[] args) {

        new VolatileRace().start();

    }

}

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...