• 线程的两种实现方式


    线程有两种实现方式

    一,Thread类

      public class Thread implements Runnable{

      }

    二,Runnable接口

      public interface Runnable{

        public abstract void run();  

    }

    区别:Thread是一个类,他继承了runnable接口,而runnable是一个接口,

    一个类只能有一个父类,但是可以有多个接口,所以在拓展型方面,ruannale有更好的拓展性,更加灵活

    Runnable还可以用于资源共享

    1. Thread的多线程示例

    下面通过示例更好的理解Thread和Runnable,借鉴网上一个例子比较具有说服性的例子。

    复制代码
     1 // ThreadTest.java 源码
     2 class MyThread extends Thread{  
     3     private int ticket=10;  
     4     public void run(){
     5         for(int i=0;i<20;i++){ 
     6             if(this.ticket>0){
     7                 System.out.println(this.getName()+" 卖票:ticket"+this.ticket--);
     8             }
     9         }
    10     } 
    11 };
    12 
    13 public class ThreadTest {  
    14     public static void main(String[] args) {  
    15         // 启动3个线程t1,t2,t3;每个线程各卖10张票!
    16         MyThread t1=new MyThread();
    17         MyThread t2=new MyThread();
    18         MyThread t3=new MyThread();
    19         t1.start();
    20         t2.start();
    21         t3.start();
    22     }  
    23 } 
    复制代码

    运行结果

    复制代码
    Thread-0 卖票:ticket10
    Thread-1 卖票:ticket10
    Thread-2 卖票:ticket10
    Thread-1 卖票:ticket9
    Thread-0 卖票:ticket9
    Thread-1 卖票:ticket8
    Thread-2 卖票:ticket9
    Thread-1 卖票:ticket7
    Thread-0 卖票:ticket8
    Thread-1 卖票:ticket6
    Thread-2 卖票:ticket8
    Thread-1 卖票:ticket5
    Thread-0 卖票:ticket7
    Thread-1 卖票:ticket4
    Thread-2 卖票:ticket7
    Thread-1 卖票:ticket3
    Thread-0 卖票:ticket6
    Thread-1 卖票:ticket2
    Thread-2 卖票:ticket6
    Thread-2 卖票:ticket5
    Thread-2 卖票:ticket4
    Thread-1 卖票:ticket1
    Thread-0 卖票:ticket5
    Thread-2 卖票:ticket3
    Thread-0 卖票:ticket4
    Thread-2 卖票:ticket2
    Thread-0 卖票:ticket3
    Thread-2 卖票:ticket1
    Thread-0 卖票:ticket2
    Thread-0 卖票:ticket1
    复制代码

    结果说明
    (01) MyThread继承于Thread,它是自定义个线程。每个MyThread都会卖出10张票。
    (02) 主线程main创建并启动3个MyThread子线程。每个子线程都各自卖出了10张票。

    2. Runnable的多线程示例

    下面,我们对上面的程序进行修改。通过Runnable实现一个接口,从而实现多线程。

    复制代码
     1 // RunnableTest.java 源码
     2 class MyThread implements Runnable{  
     3     private int ticket=10;  
     4     public void run(){
     5         for(int i=0;i<20;i++){ 
     6             if(this.ticket>0){
     7                 System.out.println(Thread.currentThread().getName()+" 卖票:ticket"+this.ticket--);
     8             }
     9         }
    10     } 
    11 }; 
    12 
    13 public class RunnableTest {  
    14     public static void main(String[] args) {  
    15         MyThread mt=new MyThread();
    16 
    17         // 启动3个线程t1,t2,t3(它们共用一个Runnable对象),这3个线程一共卖10张票!
    18         Thread t1=new Thread(mt);
    19         Thread t2=new Thread(mt);
    20         Thread t3=new Thread(mt);
    21         t1.start();
    22         t2.start();
    23         t3.start();
    24     }  
    25 }
    复制代码

    运行结果

    复制代码
    Thread-0 卖票:ticket10
    Thread-2 卖票:ticket8
    Thread-1 卖票:ticket9
    Thread-2 卖票:ticket6
    Thread-0 卖票:ticket7
    Thread-2 卖票:ticket4
    Thread-1 卖票:ticket5
    Thread-2 卖票:ticket2
    Thread-0 卖票:ticket3
    Thread-1 卖票:ticket1
    复制代码

    结果说明
    (01) 和上面“MyThread继承于Thread”不同;这里的MyThread实现了Thread接口。
    (02) 主线程main创建并启动3个子线程,而且这3个子线程都是基于“mt这个Runnable对象”而创建的。运行结果是这3个子线程一共卖出了10张票。这说明它们是共享了MyThread接口的。

    综合上面的,thread买了30张票,而runnable只卖了10张,所以说 runnable实现了资源共享 

    推荐用runnable来实现多线程

    转自:http://www.cnblogs.com/skywang12345/p/3479063.html

  • 相关阅读:
    while练习
    运算符
    作业
    [新手必看] 17个常见的Python运行时错误
    作业
    day04
    作业
    算法模板——线段树2(区间加+区间乘+区间求和)
    1798: [Ahoi2009]Seq 维护序列seq
    1708: [Usaco2007 Oct]Money奶牛的硬币
  • 原文地址:https://www.cnblogs.com/zmblog/p/8668361.html
Copyright © 2020-2023  润新知