/** * thread safely */ //public static volatile boolean run = true; /** * thread not safely */ public static boolean run = true; public static void main(String[] args) throws InterruptedException { new Thread(()->{ int i=0; while (run){ i++; Thread.yield(); System.out.println...
Thread t1 =newThread(ticket,"窗口1"); Thread t2 =newThread(ticket,"窗口2"); Thread t3 =newThread(ticket,"窗口3"); t1.start(); t2.start(); t3.start(); } } // 模拟票 classTicketimplementsRunnable { // 共100票 intticket =100; Object lock =newObject(); @Override publicvoidrun(...
*/publicstaticboolean run=true;publicstaticvoidmain(String[]args)throws InterruptedException{newThread(()->{int i=0;while(run){i++;Thread.yield();System.out.println(i+" loading...");}}).start();System.out.println("Main Thread ready to sleep...");TimeUnit.SECONDS.sleep(10);System.out...
1public class TestSynchronized { 2 public void test1() { 3 synchronized (this) { 4 int i = 5; 5 while (i-- > 0) { 6 System.out.println(Thread.currentThread().getName() + " : " + i); 7 try { 8 Thread.sleep(500); 9 } catch (InterruptedException ie) {10 }11 }12 }13 ...