java多线程中的join()、 sleep()、 yield()、 wait()函数
2015-03-14 17:18
597 查看
join()
当线程t1调用t2.join()后,t1保持锁对象,等待t2执行完,t1进入可执行状态;package thread; public class JoinRunnable implements Runnable { Thread t1 ,t2; public JoinRunnable() { t1 = new Thread(this); t1.setName("t1"); t2 = new Thread(this); t2.setName("t2"); } @Override public void run() { //如果加上synchronized锁标记,那么即使t1线程调用了t2.join();由于join不释放锁,t2还是无法按预期执行 System.out.println(Thread.currentThread().getName()); if(Thread.currentThread()==t1){ //////// try { System.out.println("t2.join();"); t2.join(); ///////////////// } catch (InterruptedException e) { e.printStackTrace(); } } for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName()); } } public static void main(String[] args){ JoinRunnable runnable = new JoinRunnable(); runnable.t1.start(); runnable.t2.start(); } }
运行结果:
t1 t2 t2.join(); t2 t2 t2 t2 t2 t2 t2 t2 t2 t2 t1 t1 t1 t1 t1 t1 t1 t1 t1 t1
----------------------------------------------------------------------------------------------------------------------------
sleep()
在线程t1中调用sleep(time),t1保持锁对象,进入阻塞状态,time完成后,t1进入可执行状态。给了低优先级线程执行的机会。package thread; public class SleepRunnable implements Runnable { @Override public synchronized void run() { for(int i=0; i<3; i++){ System.out.println(Thread.currentThread().getName()+":"+i); try{ Thread.sleep(100); //线程调用了sleep(100),由于sleep不释放锁,其它线程依然无法进入run函数 } catch(InterruptedException e){ System.out.println("Interrupted"); } } } public static void main(String[] args){ SleepRunnable myrun = new SleepRunnable(); Thread t1 = new Thread(myrun); t1.setName("t1"); Thread t2 = new Thread(myrun); t2.setName("t2"); t1.start(); t2.start(); } }
运行结果:
t1:0 t1:1 t1:2 t2:0 t2:1 t2:2
----------------------------------------------------------------------------------------------------------------------------
yield()
当线程调用yield(),线程直接进入可执行状态,不放锁package thread; public class YieldRunnable implements Runnable { @Override public void run() { //这里如果用synchronized标记,则其它线程无法进入 for(int i=0; i<3; i++){ System.out.println(Thread.currentThread().getName()+":"+i); Thread.yield();<span style="white-space:pre"> </span>////////////// } } public static void main(String[] args){ YieldRunnable myrun = new YieldRunnable(); Thread t1 = new Thread(myrun); t1.setName("t1"); Thread t2 = new Thread(myrun); t2.setName("t2"); t1.start(); t2.start(); } }运行结果:
t1:0 t2:0 t1:1 t2:1 t1:2 t2:2
----------------------------------------------------------------------------------------------------------------------------
wait()
当现在t1在调用wait()后,线程释放拥有的锁,进入线程等待池。当其他线程调用notify时,JVM从线程等待池中取走某一线程,放入锁标志等待池。package thread; public class WaitRunnable implements Runnable { Thread t1 ,t2; public WaitRunnable() { t1 = new Thread(this); t1.setName("t1"); t2 = new Thread(this); t2.setName("t2"); } @Override public synchronized void run() { if(Thread.currentThread()==t1){ System.out.println("----------"+Thread.currentThread().getName()+" wait 10"); try { wait(10);<span style="white-space:pre"> </span>//////////// } catch (InterruptedException e) { e.printStackTrace(); } } for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName()); } if(Thread.currentThread()==t2){ System.out.println("---------- t2 done"); notifyAll();<span style="white-space:pre"> </span>//////////// }else{ System.out.println("---------- t1 done"); } } public static void main(String[] args){ WaitRunnable runnable = new WaitRunnable(); runnable.t1.start(); runnable.t2.start(); } }
运行结果:
----------t1 wait 10 t2 t2 t2 t2 t2 t2 t2 t2 t2 t2 ---------- t2 done t1 t1 t1 t1 t1 t1 t1 t1 t1 t1 ---------- t1 done
相关文章推荐
- JAVA多线程之——wait/yield/sleep/join方法的区别
- JAVA多线程之——wait/yield/sleep/join方法的区别
- java多线程之yield,join,wait,sleep的区别
- JAVA中sleep,wait,yield,join函数解析_ftc
- JAVA多线程之——wait/yield/sleep/join方法的区别
- Java多线程之sleep,wait,join和yield关键字
- JAVA多线程之——wait/yield/sleep/join方法的区别
- JAVA多线程之——wait/yield/sleep/join方法的区别
- JAVA多线程之——wait/yield/sleep/join方法的区别
- 一、Java多线程之wait,notify,sleep,yield,join,suspend,resume
- Java多线程之wait,notify,sleep,,yield,join,suspend,resume
- JAVA多线程之——wait/yield/sleep/join方法的区别
- JAVA 多线程随笔 (二) sleep, yield, join, wait 和notify
- java 多线程 sleep() wait() yield() join()方法解析
- java多线程应用中常用的sleep、yield、join、wait、notify、notifyAll方法介绍
- Java多线程演示样例(模拟通话,sleep,join,yield,wait,notify,Semaphore)
- JAVA多线程之——wait/yield/sleep/join方法的区别
- JAVA多线程之——wait/yield/sleep/join方法的区别
- JAVA多线程之——wait/yield/sleep/join方法的区别
- JAVA多线程之——wait/yield/sleep/join方法的区别