Thread never stops in Java

I read Effective Javain chapter 10: Concurrency; Paragraph 66: Synchronize access to shared mutable data, the following code exists:

public class StopThread {
private static boolean stopRequested;
public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub
    System.out.println(stopRequested);
    Thread backgroundThread = new Thread(new Runnable(){

        @Override
        public void run() {
            // TODO Auto-generated method stub
            int i = 0;
            while (!stopRequested){
                i++;
            }

            System.out.println("done");
        }

    });
    backgroundThread.start();
    TimeUnit.SECONDS.sleep(1);
    stopRequested = true;
}

}

At first I think that the thread should work for one second, and then stop, since after that it is stopRequestedset to true. However, the program never stops. It will never print done. The author said

while (!stopRequested)
    i++;

will be converted to this:

if (!stopRequested)
     while(true)
         i++;

Can someone explain this to me?

And one more thing that I find is that if I change the program to this:

public class StopThread {
private static boolean stopRequested;
public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub
    System.out.println(stopRequested);
    Thread backgroundThread = new Thread(new Runnable(){

        @Override
        public void run() {
            // TODO Auto-generated method stub
            int i = 0;
            while (!stopRequested){
                i++;
                System.out.println(i);
            }

            System.out.println("done");
        }

    });
    backgroundThread.start();
    TimeUnit.SECONDS.sleep(1);
    stopRequested = true;
}

}

The program runs for 1 second and stops as expected. What is the difference here?

+4
source share
2 answers

, ().

,

while (!stopRequested)
    i++;

if (!stopRequested)
     while(true)
         i++;

Java stopRequested ( ) . , , " " . . , , stopRequested.

, :

  • stopRequested volatile,
  • , , stopRequested, synchronized, ,
  • Lock , synchronized,
  • - concurrency, " ".

, , , .

, , , ....

. Java , .

, , JVM , , ( ) . , 99,9% . , 100% . , , -. JLS .


, -, , System.out PrintWriter println. , " ".

+8

, ( ) -, :-). , ( ):

:

while (!done)
  i++;

:

if (!done)
  while (true)
    i++;

, ( , 261-264, . , !), . , :

  • StopThread .
  • Linux- JRE 1.8.0_72.
  • ! , , .
  • " ", , . kill -3 JVM pid, , . ( ):
"DestroyJavaVM" #10 prio=5 os_prio=0 tid=0x00007fd678009800 nid=0x1b35 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Thread-0" #9 prio=5 os_prio=0 tid=0x00007fd6780f6800 nid=0x1b43 runnable [0x00007fd64b5be000]
   java.lang.Thread.State: RUNNABLE
  at StopThread$1.run(StopThread.java:14)
  at java.lang.Thread.run(Thread.java:745)

"Service Thread" #8 daemon prio=9 os_prio=0 tid=0x00007fd6780c9000 nid=0x1b41 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

, , , , - . top :

top cmd output.

, ( ) (100%!) - , , Java, - . ? , . CPU -, , , . , , stopRequested , ( ) . , , , ! , , ( , :-)).

... main thread (, , , ) stopRequested = true?

, !

, , Thread-0 ?

. , , , .

, , :

private static boolean stopRequested;

! , (, ...). underspecification . , , ( ), Thread-0, .

, . , , , .

? . Herlihy Shavit. - , , .

+3

All Articles