public class StudyWhileLoopMain { private volatile static int tag = 0; public static void main(String[] args) { System.out.println("主函数开始执行"); MyThread myThread = new MyThread(); myThread.start(); System.out.println("线程已经开始"); /** tag如果不加volatile修复则一直不结束 */ while (tag == 0); System.out.println("标记通过"); } protected static class MyThread extends java.lang.Thread { @Override public void run() { super.run(); System.out.println("线程开始"); try { Thread.sleep(9000); } catch (java.lang.InterruptedException e) { e.printStackTrace(); } System.out.println("线程赋值标记为1"); tag = 1; } } }
08-03
1308

09-16
3269

03-26