public final void wait(long timeout, int nanos) throws InterruptedException { if (timeout < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (nanos < 0 || nanos > 999999) { throw new IllegalArgumentException( "nanosecond timeout value out of range"); } if (nanos >= 500000 || (nanos != 0 && timeout == 0)) { timeout ; } wait(timeout); }
package com.paddx.test.concurrent; public class WaitTest { public void testWait(){ System.out.println("Start-----"); try { wait(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("End-------"); } public static void main(String[] args) { final WaitTest test = new WaitTest(); new Thread(new Runnable() { @Override public void run() { test.testWait(); } }).start(); } }
Start----- Exception in thread "Thread-0" java.lang.IllegalMonitorStateException at java.lang.Object.wait(Native Method) at com.paddx.test.concurrent.WaitTest.testWait(WaitTest.java:8) at com.paddx.test.concurrent.WaitTest$1.run(WaitTest.java:20) at java.lang.Thread.run(Thread.java:745)
package com.paddx.test.concurrent; public class WaitTest { public synchronized void testWait(){//增加Synchronized关键字 System.out.println("Start-----"); try { wait(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("End-------"); } public static void main(String[] args) { final WaitTest test = new WaitTest(); new Thread(new Runnable() { @Override public void run() { test.testWait(); } }).start(); } }
Start----- End-------
package com.paddx.test.concurrent; public class NotifyTest { public synchronized void testWait(){ System.out.println(Thread.currentThread().getName() " Start-----"); try { wait(0); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() " End-------"); } public static void main(String[] args) throws InterruptedException { final NotifyTest test = new NotifyTest(); for(int i=0;i<5;i ) { new Thread(new Runnable() { @Override public void run() { test.testWait(); } }).start(); } synchronized (test) { test.notify(); } Thread.sleep(3000); System.out.println("-----------分割线-------------"); synchronized (test) { test.notifyAll(); } } }
Thread-0 Start----- Thread-1 Start----- Thread-2 Start----- Thread-3 Start----- Thread-4 Start----- Thread-0 End------- -----------分割线------------- Thread-4 End------- Thread-3 End------- Thread-2 End------- Thread-1 End-------
package com.paddx.test.concurrent; public class SleepTest { public synchronized void sleepMethod(){ System.out.println("Sleep start-----"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Sleep end-----"); } public synchronized void waitMethod(){ System.out.println("Wait start-----"); synchronized (this){ try { wait(1000); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("Wait end-----"); } public static void main(String[] args) { final SleepTest test1 = new SleepTest(); for(int i = 0;i<3;i ){ new Thread(new Runnable() { @Override public void run() { test1.sleepMethod(); } }).start(); } try { Thread.sleep(10000);//暂停十秒,等上面程序执行完成 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("-----分割线-----"); final SleepTest test2 = new SleepTest(); for(int i = 0;i<3;i ){ new Thread(new Runnable() { @Override public void run() { test2.waitMethod(); } }).start(); } } }
Sleep start----- Sleep end----- Sleep start----- Sleep end----- Sleep start----- Sleep end----- -----分割线----- Wait start----- Wait start----- Wait start----- Wait end----- Wait end----- Wait end-----
package com.paddx.test.concurrent; public class YieldTest implements Runnable { @Override public void run() { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } for(int i=0;i<5;i ){ System.out.println(Thread.currentThread().getName() ": " i); Thread.yield(); } } public static void main(String[] args) { YieldTest runn = new YieldTest(); Thread t1 = new Thread(runn,"FirstThread"); Thread t2 = new Thread(runn,"SecondThread"); t1.start(); t2.start(); } }
FirstThread: 0 SecondThread: 0 FirstThread: 1 SecondThread: 1 FirstThread: 2 SecondThread: 2 FirstThread: 3 SecondThread: 3 FirstThread: 4 SecondThread: 4
/** * A hint to the scheduler that the current thread is willing to yield * its current use of a processor. The scheduler is free to ignore this * hint. * * <p> Yield is a heuristic attempt to improve relative progression * between threads that would otherwise over-utilise a CPU. Its use * should be combined with detailed profiling and benchmarking to * ensure that it actually has the desired effect. * * <p> It is rarely appropriate to use this method. It may be useful * for debugging or testing purposes, where it may help to reproduce * bugs due to race conditions. It may also be useful when designing * concurrency control constructs such as the ones in the * {@link java.util.concurrent.locks} package. */
public final void join() throws InterruptedException { join(0); } public final synchronized void join(long millis) throws InterruptedException { long base = System.currentTimeMillis(); long now = 0; if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (millis == 0) { while (isAlive()) { wait(0); } } else { while (isAlive()) { long delay = millis - now; if (delay <= 0) { break; } wait(delay); now = System.currentTimeMillis() - base; } } } public final synchronized void join(long millis, int nanos) throws InterruptedException { if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (nanos < 0 || nanos > 999999) { throw new IllegalArgumentException( "nanosecond timeout value out of range"); } if (nanos >= 500000 || (nanos != 0 && millis == 0)) { millis ; } join(millis); }
package com.paddx.test.concurrent; public class JoinTest implements Runnable{ @Override public void run() { try { System.out.println(Thread.currentThread().getName() " start-----"); Thread.sleep(1000); System.out.println(Thread.currentThread().getName() " end------"); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) { for (int i=0;i<5;i ) { Thread test = new Thread(new JoinTest()); test.start(); } System.out.println("Finished~~~"); } }
Thread-0 start----- Thread-1 start----- Thread-2 start----- Thread-3 start----- Finished~~~ Thread-4 start----- Thread-2 end------ Thread-4 end------ Thread-1 end------ Thread-0 end------ Thread-3 end------
package com.paddx.test.concurrent; public class JoinTest implements Runnable{ @Override public void run() { try { System.out.println(Thread.currentThread().getName() " start-----"); Thread.sleep(1000); System.out.println(Thread.currentThread().getName() " end------"); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) { for (int i=0;i<5;i ) { Thread test = new Thread(new JoinTest()); test.start(); try { test.join(); //调用join方法 } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("Finished~~~"); } }
Thread-0 start----- Thread-0 end------ Thread-1 start----- Thread-1 end------ Thread-2 start----- Thread-2 end------ Thread-3 start----- Thread-3 end------ Thread-4 start----- Thread-4 end------ Finished~~~