Thread--Thread和Runnable

本文介绍了Java中实现多线程的两种主要方法:继承Thread类和实现Runnable接口。通过示例代码展示了如何创建和启动线程,比较了两种方法的区别。
在Java中常用的实现多线程的两种方式是使用Thread类或者实现Runnable接口。
Runnable是一个接口,并且只包含了一个run()方法。

基于Java8的Runnable源码:

[java]  view plain  copy
  1. @FunctionalInterface  
  2. public interface Runnable {  
  3. /** 
  4.      * When an object implementing interface <code>Runnable</code> is used 
  5.      * to create a thread, starting the thread causes the object's 
  6.      * <code>run</code> method to be called in that separately executing 
  7.      * thread. 
  8.      * <p> 
  9. * The general contract of the method <code>run</code> is that it may 
  10.      * take any action whatsoever. 
  11.      * 
  12.      * @see     java.lang.Thread#run() 
  13.      */  
  14. public abstract void run();  
  15. }  
Runnable示例代码:实现Runnable接口,再通过Thread的构造函数创建线程。

[java]  view plain  copy
  1. public class MyThread  implements Runnable{  
  2.   
  3. private int ticket=10;  
  4.     public void run() {  
  5.         // TODO 自动生成的方法存根  
  6.         for(int i=0;i<20;i++)  
  7.         {  
  8.             if(this.ticket>0)  
  9.             {  
  10.                 System.out.println(Thread.currentThread().getName()+" 卖票:ticket"+this.ticket--);  
  11.             }  
  12.         }  
  13.     }  
  14. }  
[java]  view plain  copy
  1. public class Hello {  
  2.   
  3. public static void main(String [] agrs)  
  4.  {  
  5.         MyThread myThread=new MyThread();  
  6.         Thread t1=new Thread(myThread);  
  7.         Thread t2=new Thread(myThread);  
  8.         Thread t3=new Thread(myThread);  
  9.   
  10.         t1.start();  
  11.         t2.start();  
  12.         t3.start();  
  13.     }  
  14. }  
结果:
Thread-0 卖票:ticket10
Thread-2 卖票:ticket10
Thread-1 卖票:ticket10

Thread本身是一个类,然后它实现了Runnable接口,因此在使用Thread时我们只需要继承Thread并重写它的run()方法就好。
Thread示例:
[java]  view plain  copy
  1. public class NewThread  extends Thread{  
  2. private int ticket=10;  
  3.     public void run()  
  4.     {  
  5.         for(int i=0;i<20;i++)  
  6.         {  
  7.             if(this.ticket>0)  
  8.             {  
  9.                 System.out.println(this.getName()+" 卖票:ticket"+this.ticket--);  
  10.             }  
  11.         }  
  12.     }  
  13. }  
[java]  view plain  copy
  1. public class Hello {  
  2.   
  3. public static void main(String [] agrs)  
  4.     {  
  5.         NewThread t1=new NewThread();  
  6.         NewThread t2=new NewThread();  
  7.         NewThread t3=new NewThread();  
  8.   
  9.         t1.start();  
  10.         t2.start();  
  11.         t3.start();  
  12.     }  
  13. }  
运行结果:
Thread-0 卖票:ticket10
Thread-0 卖票:ticket9
Thread-0 卖票:ticket8
Thread-0 卖票:ticket7
Thread-0 卖票:ticket6
Thread-0 卖票:ticket5
Thread-0 卖票:ticket4
Thread-0 卖票:ticket3
Thread-0 卖票:ticket2
Thread-2 卖票:ticket10
Thread-2 卖票:ticket9
Thread-2 卖票:ticket8
Thread-2 卖票:ticket7
Thread-2 卖票:ticket6
Thread-2 卖票:ticket5
Thread-2 卖票:ticket4
Thread-2 卖票:ticket3
Thread-2 卖票:ticket2
Thread-2 卖票:ticket1
Thread-1 卖票:ticket10
Thread-1 卖票:ticket9
Thread-1 卖票:ticket8
Thread-1 卖票:ticket7
Thread-1 卖票:ticket6
Thread-1 卖票:ticket5
Thread-1 卖票:ticket4
Thread-1 卖票:ticket3
Thread-1 卖票:ticket2
Thread-1 卖票:ticket1
Thread-0 卖票:ticket1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值