(1)传统线程技术回顾

1,java中线程的创建可以有两种方法:1、继承Thread类 2、实现Runnable接口

两者区别:

(1)、继承Thread类创建线程的方法, 在继承了Thread类之后, 不能再继承其他的方法, (在java中支持单继承),这样灵活性就不如实现Runnable接口来创建线程的方法了;

(2)、使用实现Runnable接口来创建线程可以达到资源共享!(继承Thread类创建线程也可以实现资源共享,但是比较的麻烦。)

在我们创建线程的时候都会优先采用实现Runnable接口的方法。

[java]  view plain copy print ?
  1. package com.thread;  
  2.   
  3. public class TestThread2 {  
  4.   
  5.     /** 
  6.      * @param args 
  7.      */  
  8.     public static void main(String[] args) {  
  9.         Runner r = new Runner();  
  10.         r.start();  
  11.           
  12.         for(int i=0; i<100; i++) {  
  13.             System.out.println("Main Thread:------" + i);  
  14.         }  
  15.     }  
  16.   
  17. }  
  18. class Runner extends Thread {  
  19.     public void run() {  
  20.         for(int i=0; i<100; i++) {     
  21.             System.out.println("Runner1 :" + i);  
  22.         }  
  23.     }  
  24. }  
[java]  view plain copy print ?
  1. package com.thread;  
  2.   
  3. public class TestThread3 {  
  4.   
  5.     /** 优先考虑的方式。 
  6.      * @param args 
  7.      */  
  8.     public static void main(String[] args) {  
  9.         Runner2 r = new Runner2();  
  10.         Thread t1 = new Thread(r);  
  11.         Thread t2 = new Thread(r);  
  12.         t1.start();  
  13.         t2.start();  
  14.     }  
  15. }  
  16.   
  17. class Runner2 implements Runnable{  
  18.   
  19.     @Override  
  20.     public void run() {  
  21.         for(int i=0;i<30;i++){  
  22.             System.out.println("No:" + i);  
  23.         }  
  24.           
  25.     }  
  26.       
  27. }  





[java]  view plain copy print ?
  1. <span style="font-size:16px;">package com.itm.thread;  
  2.   
  3. public class TraditionalThread {  
  4.   
  5.     /** 
  6.      * @param args 
  7.      */  
  8.     public static void main(String[] args) {  
  9.             Thread thread = new Thread() {  
  10.                 @Override  
  11.                 public void run() {  
  12.                     while(true){  
  13.                         try {  
  14.                             Thread.sleep(500);  
  15.                         } catch (InterruptedException e) {  
  16.                             e.printStackTrace();  
  17.                         }  
  18.                         System.out.println("1  " + Thread.currentThread().getName());  
  19.                         System.out.println("2  " + this.getName());  
  20.                     }  
  21.                 }  
  22.             };  
  23.             thread.start();  
  24.               
  25.               
  26.             Thread thread2 = new Thread(new Runnable(){  
  27.                 @Override  
  28.                 public void run() {  
  29.                     while(true){  
  30.                         try {  
  31.                             Thread.sleep(500);  
  32.                         } catch (InterruptedException e) {  
  33.                             e.printStackTrace();  
  34.                         }  
  35.                         System.out.println("1  " + Thread.currentThread().getName());  
  36.                         // this 就不代表线程了。因为在 Runnable 对象里面。  
  37. //                      System.out.println("2" + this.getName());  
  38.                     }  
  39.                       
  40.                 }  
  41.             });  
  42.             thread2.start();  
  43.               
  44. /******************************************************************************** 
  45.  *  
  46.  * 注释源码说明:利用了  面向对象的  覆盖技术。 
  47.  *  
  48.  *  
  49.  * 调用 start()方法,start()方法 会找  当前对象的run()方法  
  50.  * (1) 如果自己没有 就找父类中的run()方法。 
  51.  *          父类的run()方法 会找 Runnable。 
  52.  * (2) 如果 自己有 run()方法,就把 父类的覆盖了。 
  53.  *  
  54.  *  
  55.  */  
  56.               
  57.             new Thread(  
  58.                     new Runnable() {  
  59.                           
  60.                         @Override  
  61.                         public void run() {  
  62.                             while(true){  
  63.                                 try {  
  64.                                     Thread.sleep(500);  
  65.                                 } catch (InterruptedException e) {  
  66.                                     e.printStackTrace();  
  67.                                 }  
  68.                                 System.out.println("Runnable :" + Thread.currentThread().getName());  
  69.                             }                             
  70.                         }  
  71.                     }  
  72.             ){  
  73.                 public void run() {  
  74.                     while(true){  
  75.                         try {  
  76.                             Thread.sleep(500);  
  77.                         } catch (InterruptedException e) {  
  78.                             e.printStackTrace();  
  79.                         }  
  80.                         System.out.println("Thread :  " + Thread.currentThread().getName());  
  81.                     }  
  82.                 };  
  83.             }.start();  
  84.               
  85.     }  
  86.       
  87. }</span>  

关于  上面注释源码的:

[java]  view plain copy print ?
  1. <span style="color:#990000;">(1):  
  2. public void run() {  
  3.     if (target != null) { // 如果 target不是为空 就会执行 Runnable的run(); 因为:target是 Runnable的 一个变量。  
  4.         target.run();  
  5.     }  
  6. }</span>  
  7.   
  8. (2) :    
  9.             /* What will be run. */  
  10.         private Runnable target;  
  11.   
  12.   
  13.     ------------------------------------------------------------------------------------------------------  
  14.   
  15.   
  16.      /** 
  17.      * Initializes a Thread. 
  18.      * 
  19.      * @param g the Thread group 
  20.      * @param target the object whose run() method gets called 
  21.      * @param name the name of the new Thread 
  22.      * @param stackSize the desired stack size for the new thread, or 
  23.      *        zero to indicate that this parameter is to be ignored. 
  24.      */  
  25.   <span style="color:#990000;"><strong>  private void init(ThreadGroup g, Runnable target, String name,  
  26.                       long stackSize) </strong></span>{  
  27.     Thread parent = currentThread();  
  28.     SecurityManager security = System.getSecurityManager();  
  29.     if (g == null) {  
  30.         /* Determine if it's an applet or not */  
  31.           
  32.         /* If there is a security manager, ask the security manager 
  33.            what to do. */  
  34.         if (security != null) {  
  35.         g = security.getThreadGroup();  
  36.         }  
  37.   
  38.         /* If the security doesn't have a strong opinion of the matter 
  39.            use the parent thread group. */  
  40.         if (g == null) {  
  41.         g = parent.getThreadGroup();  
  42.         }  
  43.     }  
  44.   
  45.     /* checkAccess regardless of whether or not threadgroup is 
  46.            explicitly passed in. */  
  47.     g.checkAccess();  
  48.   
  49.     /* 
  50.      * Do we have the required permissions? 
  51.      */  
  52.     if (security != null) {  
  53.         if (isCCLOverridden(getClass())) {  
  54.             security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);  
  55.         }  
  56.     }  
  57.   
  58.   
  59.         g.addUnstarted();  
  60.   
  61.     this.group = g;  
  62.     this.daemon = parent.isDaemon();  
  63.     this.priority = parent.getPriority();  
  64.     this.name = name.toCharArray();  
  65.     if (security == null || isCCLOverridden(parent.getClass()))  
  66.         this.contextClassLoader = parent.getContextClassLoader();  
  67.     else  
  68.         this.contextClassLoader = parent.contextClassLoader;  
  69.     this.inheritedAccessControlContext = AccessController.getContext();  
  70.     this.target = target;  
  71.     setPriority(priority);  
  72.         if (parent.inheritableThreadLocals != null)  
  73.         this.inheritableThreadLocals =  
  74.         ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);  
  75.         /* Stash the specified stack size in case the VM cares */  
  76.         this.stackSize = stackSize;  
  77.   
  78.         /* Set thread ID */  
  79.         tid = nextThreadID();  
  80.     }  
  81.   
  82.     public void run() {  
  83.     if (target != null) {  
  84.         target.run();  
  85.     }  
  86.     }  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值