In short: Shared variables between threads are volatile immediately.

If one thread changes the value of a variable, the new value is immediately visible to other threads. Shared variables between threads need to be Volatile to ensure that changes made by one thread are immediately visible to other threads. 48.750 49.589 Offset: 161 Offset: 31.162 31.135 Offset: 27 Interrupt 38.400 39.177 Offset: 223

class TestThread extends Thread{ public volatile boolean isInterrupted=true; @Override public void run() { while (isInterrupted){ System.out.println("11"); Log.e("wy", "run: "); } // Thread interrupts with interrupt method // while (! isInterrupted()){ // System.out.println("11"); // Log.e("wy", "run: "); / /}}}Copy the code
 TestThread mTestThread=new TestThread();
        mTestThread .start();
        try {
            Thread.sleep(1000);
            mTestThread.isInterrupted=false;
//            mTestThread.interrupt();
        } catch (InterruptedException mE) {
            mE.printStackTrace();
        }
Copy the code