再学java基础(11) java 线程(sleep,join,yield) 经典实例。

[java]  view plain copy print ?
  1. <span style="font-size:16px;"><strong>package com.thread;  
  2.   
  3. public class TestThreadShutDown {  
  4.   
  5.     /** 
  6.      *  
  7.      * 友情 关闭,而不可以用 stop()这样粗暴的方法。 
  8.      *  
  9.      * @param args 
  10.      */  
  11.     public static void main(String[] args) {  
  12.         Runner4 r = new Runner4();  
  13.         Thread t = new Thread(r);  
  14.         t.start();  
  15.           
  16.         for(int i=0;i<100000;i++) {  
  17.             if(i%10000 == 0 & i>0){  
  18.                 System.out.println("in thread main i=" + i);  
  19.             }  
  20.         }  
  21.           
  22.         System.out.println("Thread main is over");  
  23.         r.shutDown();  
  24.     }  
  25.   
  26. }  
  27. class Runner4 implements Runnable{  
  28.     private boolean flag = true;  
  29.     @Override  
  30.     public void run() {  
  31.         int i = 0;  
  32.         while(flag == true){  
  33.             System.out.println("  " + i);  
  34.         }  
  35.           
  36.     }  
  37.       
  38.     public void shutDown(){  
  39.         flag = false;  
  40.     }  
  41.       
  42. }</strong></span>  


[java]  view plain copy print ?
  1. <span style="font-size:16px;"><strong>package com.thread;  
  2.   
  3. /************************ 
  4.  *  
  5.     join()方法使当前线程停下来等待,直至另一个调用join方法的线程终止。 
  6.         值得注意的是,线程的在被激活后不一定马上就运行. 
  7.             而是进入到可运行线程的队列中。 
  8.         但是join()可以通过interrupt()方法打断线程的暂停状态, 
  9.      
  10.         从而使线程立刻抛出InterruptedException。   
  11.  *  
  12.  * @author wang.dm 
  13.  * 
  14.  */  
  15. public class Test_Join_1 {  
  16.   
  17.     /** 
  18.      * @param args 
  19.      */  
  20.     public static void main(String[] args) {  
  21.         Thread t = new Thread(new RunnableIm());  
  22.         t.start();  
  23.           
  24.         try {  
  25.             // 当main 线程调用t.join 时,main 线程等待t 线程 ,等待时间是1000   
  26.             t.join(1000); // 主线程 只等1 秒,就等待一秒钟,不管子线程什么时候结束。  
  27.             System.out.println("JOIN 结束");  
  28.         } catch (InterruptedException e) {  
  29.             e.printStackTrace();  
  30.         }  
  31.     }  
  32.   
  33. }  
  34. class RunnableIm implements Runnable{  
  35.   
  36.     @Override  
  37.     public void run() {  
  38.           
  39.         try {  
  40.             System.out.println("开始睡觉……");  
  41.             Thread.sleep(9000); // T 线程睡 10秒钟。而 main 线程 只等待一秒钟。  
  42.             System.out.println("停止睡觉……");  
  43.         } catch (InterruptedException e) {  
  44.             e.printStackTrace();  
  45.         }  
  46.           
  47.           
  48.     }  
  49.       
  50. }</strong></span>  

[java]  view plain copy print ?
  1. <span style="font-size:16px;"><strong>package com.thread;  
  2.   
  3. /* 
  4.  *  
  5.      
  6.     在main方法中 通过new ThreadTest(t).start();实例化ThreadTest 线程对象,  
  7.     它在holdThreadLock()方法中,通过 synchronized (thread),获取线程对象t的锁,并Sleep(9000)后释放, 
  8.     这就意味着,即使 
  9.     main方法t.join(1000),等待一秒钟,  
  10.         它必须等待ThreadTest 线程释放t锁后才能进入wait方法中, 
  11.         它实际等待时间是9000+1000 MS 
  12.      
  13.     运行结果是: 
  14.         getObjectLock 
  15.         Begin sleep 
  16.         End sleep 
  17.         ReleaseObjectLock 
  18.         joinFinish 
  19.      
  20.          
  21.          
  22.     */  
  23. public class JoinTest {  
  24.     public static void main(String[] args) {  
  25.           
  26.         Thread t = new Thread(new RunnableImpl());  
  27.         new ThreadTest(t).start();  
  28.         t.start();  
  29.         try {  
  30.             t.join(1000);  
  31.             System.out.println("joinFinish");  
  32.         } catch (InterruptedException e) {  
  33.             e.printStackTrace();  
  34.   
  35.         }  
  36.     }  
  37. }  
  38.   
  39. class ThreadTest extends Thread {  
  40.   
  41.     Thread thread;  
  42.   
  43.     public ThreadTest(Thread thread) {  
  44.         this.thread = thread;  
  45.     }  
  46.   
  47.     @Override  
  48.     public void run() {  
  49.         holdThreadLock();  
  50.     }  
  51.   
  52.     public void holdThreadLock() {  
  53.         synchronized (thread) { // 获取 线程 t 的锁。  
  54.             System.out.println("getObjectLock");  
  55.             try {  
  56.                 Thread.sleep(9000);  
  57.   
  58.             } catch (InterruptedException ex) {  
  59.                 ex.printStackTrace();  
  60.             }  
  61.             System.out.println("ReleaseObjectLock");  
  62.         }  
  63.   
  64.     }  
  65. }  
  66.   
  67. class RunnableImpl implements Runnable {  
  68.   
  69.     @Override  
  70.     public void run() {  
  71.         try {  
  72.             System.out.println("Begin sleep");  
  73.             Thread.sleep(2000);  
  74.             System.out.println("End sleep");  
  75.         } catch (InterruptedException e) {  
  76.             e.printStackTrace();  
  77.         }  
  78.   
  79.     }  
  80. }  
  81. </strong></span>  

[java]  view plain copy print ?
  1. <span style="font-size:16px;"><strong>package com.thread;  
  2.   
  3. import java.util.Date;  
  4.   
  5. public class TestInterrupt {  
  6.   
  7.     /** 
  8.       
  9.      interrupt()  
  10.         interrupt()中断线程。需要注意的是,InterruptedException是线程自己从内部抛出的, 
  11.         并不是interrupt()方法抛出的。 
  12.         对某一线程调用interrupt()时,如果该线程正在执行普通的代码. 
  13.         那么该线程根本就不会抛出InterruptedException。 
  14.         但是,一旦该线程进入到wait()/sleep()/join()后,就会立刻抛出InterruptedException。 
  15.  
  16.      * @param args 
  17.      */  
  18.     public static void main(String[] args) {  
  19.         MyThread thread = new MyThread();  
  20.         thread.start();  
  21.           
  22.         try {  
  23.             /************************** 
  24.              *  
  25.              sleep()  
  26.                 在指定的毫秒数内让当前正在执行的线程休眠(暂停执行), 
  27.                     此操作受到系统计时器和调度程序精度和准确性的影响。  
  28.  
  29.             由于sleep()方法是Thread类的方法,因此它不能改变对象的机锁。 
  30.                 所以当在一个Synchronized方法中调用sleep()时,线程虽然休眠了. 
  31.                     但是对象的机锁没有被释放,其他线程仍然无法访问这个对象。 
  32.             sleep()方法不需要在同步的代码块中执行。 
  33.              
  34.             但是sleep()可以通过interrupt()方法打断线程的暂停状态,从而使线程立刻抛出InterruptedException。  
  35.              */  
  36.             Thread.sleep(10000);  //在哪个线程里调用 sleep方法 就让那个线程睡眠。主线程睡眠了。  
  37.         } catch (InterruptedException e) {  
  38.             thread.interrupt();  
  39.         }  
  40.   
  41.     }  
  42.   
  43. }  
  44.   
  45. class MyThread extends Thread {  
  46.     boolean flag = true;  
  47.     public void run() {  
  48.         while(flag){  
  49.             System.out.println("====" + new Date() + "=====");  
  50.             try {  
  51.                 sleep(1000);  
  52.             } catch (InterruptedException e) {  
  53.                 return;  
  54.             }  
  55.         }  
  56.     }  
  57. }</strong></span>  

[java]  view plain copy print ?
  1. <span style="font-size:16px;"><strong>package com.thread;  
  2.   
  3. public class TestYield {  
  4.   
  5.     /** 
  6.      * @param args 
  7.      */  
  8.     public static void main(String[] args) {  
  9.         MyThread3 t1 = new MyThread3("t1");  
  10.         MyThread3 t2 = new MyThread3("t2");  
  11.         t1.start();  
  12.         t2.start();  
  13.           
  14.   
  15.     }  
  16.   
  17. }  
  18. class MyThread3 extends Thread {  
  19.     MyThread3(String s){  
  20.         super();  
  21.     }  
  22.     public void run(){  
  23.         for(int i=0;i<=100;i++) {  
  24.             System.out.println(getName() + " : " + i);  
  25.             if(i%10 == 0){  
  26.                 //Yield()方法是停止当前线程,让同等优先权的线程运行。  
  27.                 //如果没有同等优先权的线程,那么Yield()方法将不会起作用。   
  28.                 yield();  
  29.             }  
  30.         }  
  31.     }  
  32. }</strong></span>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值