JAVA 线程的例子

  1 package test.capture;
  2 
  3 import java.io.IOException;
  4 
  5 public class MultiThread {
  6 
  7     /**
  8      * JAVA线程很好的例子
  9      */
 10     public static void main(String[] args) {
 11         
 12         System.out.println("-----------我是主线程---------!");
 13         //创建线程thread1
 14         ThreadUserExtends thread1=new ThreadUserExtends();
 15         //创建thread2时以实现了Runnable接口的ThreadUseRunnable类实例为参数
 16         Thread thread2 = new Thread(new ThreadUserRunable(),"SecondThread");
 17          
 18         /**
 19          * thread1.setPriority(6);//设置thread1的优先级为6
 20          * 优先级将决定CPU空出时,处于就绪状态的线程谁先占领CPU开始运行
 21          * 优先级范围1到10,MIN_PRIORITY,MAX_PRIORITY,NORM_PAIORITY
 22          * 新线程继承创建她的父线程优先级,父线程通常有普通优先级即5NORM_PRIORITY
 23          */
 24         thread1.start();//启动线程thread1使之处于就绪状态
 25         
 26         System.out.println("-----------主线程将挂起7秒!-----------");
 27         try{
 28            Thread.sleep(7000);//主线程挂起7秒
 29         }catch (InterruptedException e){
 30             return;
 31         }
 32         
 33         System.out.println("-----------又回到了主线程!-----------");
 34         
 35          if(thread1.isAlive()){
 36              thread1.stop();//如果thread1还存在则杀掉他
 37              System.out.println("-----------thread1休眠过长,主线程杀掉了thread1!-----------");
 38          }else{
 39             System.out.println("-----------主线程没发现thread1,thread1已醒顺序执行结束了!-----------");
 40          }
 41          
 42          thread2.start();//启动thread2
 43          System.out.println("-----------主线程又将挂起7秒!-----------");
 44          try{
 45              Thread.sleep(7000);//主线程挂起7秒
 46          }catch (InterruptedException e){
 47              return;
 48          }
 49          
 50          System.out.println("-----------又回到了主线程!-----------");
 51          if(thread2.isAlive()){
 52              thread2.stop();//如果thread2还存在则杀掉他
 53              System.out.println("-----------thread2休眠过长,主线程杀掉了thread2!-----------");
 54          }else{
 55              System.out.println("-----------主线程没发现thread2,thread2已醒顺序执行结束了!-----------");
 56          }
 57          
 58          System.out.println("-----------程序结束按任意键继续!-----------");
 59          try{
 60              System.in.read();
 61          }catch (IOException e){
 62              System.out.println(e.toString());
 63          }
 64     }
 65 }
 66 
 67 /**
 68  * 通过继承Thread类,并实现它的抽象方法run()
 69  * 适当时候创建这一Thread子类的实例来实现多线程机制
 70  * 一个线程启动后(也就是进入就绪状态)一旦获得CPU将自动调用它的run()方法
 71  */
 72 class ThreadUserExtends extends Thread{
 73     
 74     ThreadUserExtends(){ 
 75         //无参的构造函数
 76     }
 77     
 78     public void run(){
 79         System.out.println("+++++++++++我是Thread子类的线程实例!+++++++++++++");
 80         System.out.println("+++++++++++我将挂起10秒!+++++++++++++");
 81         System.out.println("+++++++++++回到主线程,请稍等,刚才主线程挂起可能还没醒过来!+++++++++++++");
 82         /**
 83          * 如果该run()方法顺序执行完了,线程将自动结束,而不会被主线程杀掉
 84          * 但如果休眠时间过长,则线程还存活,可能被stop()杀掉
 85          */
 86         try{
 87             sleep(10000);//挂起10秒
 88         }catch(InterruptedException e){
 89             e.printStackTrace();
 90             return;
 91         }
 92     }    
 93 }
 94 
 95 /**
 96  *通过实现Runnable接口中的run()方法,再以这个实现了run()方法的类
 97  *为参数创建Thread的线程实例
 98  *以这个实现了Runnable接口中run()方法的类为参数创建Thread类的线程实例
 99  */
100 class ThreadUserRunable implements Runnable{
101     
102     ThreadUserRunable(){//构造函数
103         
104     }
105     
106     public void run(){
107         System.out.println("***********我是Thread类的线程实例并以实现了Runnable接口的类为参数!***********");
108         System.out.println("***********我将挂起10秒!***********");
109         System.out.println("***********回到主线程,请稍等,刚才主线程挂起可能还没醒过来!***********");
110         /**
111          * 如果该run()方法顺序执行完了,线程将自动结束,而不会被主线程杀掉
112          * 但如果休眠时间过长,则线程还存活,可能被stop()杀掉
113          */
114         try{
115             Thread.sleep(10000);//挂起10秒
116         }catch(InterruptedException e){
117             e.printStackTrace();
118             return;
119         }
120     }
121 }
122 
123 
124 

posted on 2009-07-17 21:47 彭伟 阅读(206) 评论(0)  编辑  收藏 所属分类: java技术分区


只有注册用户登录后才能发表评论。


网站导航:
 
<2009年7月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿(3)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜