黑马程序员——线程实现的两种方法

---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------

线程实现的两种方法:

1、继承Thread。

2、实现Runnable接口,同时覆run()方法。

  继承Thread实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class  MyThread extends  Thread {
     private  String name;
     public  MyThread(String name)
     {
         this .name = name;
     }
     public  void  run()
     {
         for ( int  i= 0 ;i< 10 ;i++)
             System.out.println( this .name+ "---线程启动" +i);
     }
}
public  class  MyThreadDemo {
     public  static  void  main(String args[])
     {
         MyThread mt = new  MyThread( "自定义线程" );
         mt.start();
     }
}

  实现Runnable接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class  MyThread1 implements  Runnable
{
     public  void  run() {
         for ( int  i= 0 ;i< 10 ;i++) {
             System.out.println( "--线程启动--" +i);
         }
                                
     }
}
 
 
public  class  MyThreadDemo1 {
     public  static  void  main(String args[]) {
         MyThread1
         mt = new  MyThread1();
         Thread t = new  Thread(mt);
         t.start();
         
     }
}

 

  ★在这里为什么要使用start()方法启动线程呢?

通过查阅可以看到Runnable接口如下:

1
2
3
4
5
interface  Runnable
{
     public  void  run();
}
  .......... Thread implements  Runnable {    //其他方法   publicc void run(){}   public native void start(); }  

   Runable子类中并没有start()方法,而只有Thread类中才有,如果直接调用run()方法,

程序将会运行完run()方法体中的语句再运行后面的语句,根本达不到多线程的目的;

调用没有方法体的start()方法,native关键字声明的方法没有方法体,使用此关键字表示可调

用操作系统的底层函数(那么这样的技术又称为JNI技术),而且此方法在执行时将调用run()

方法(由系统默认调用)。

  ★在使用Runnable子类中并没有start()方法,而只有Thread类中才有,在Thread类中存在

以下构造方法:

  public Thread(Runnable target)

此构造方法接收Runnabled 子类实例。

  ★两种实例的区别(实现Runnable的好处)

1、避免单继承。

2、实现资源共享。(以卖票为例)

 

  Thread在卖张票三个线程同时卖票共卖了15张票(资源不共享)如下: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class  Ticket extends  Thread {
     private  int  tick = 5 ;
     public  void  run(){
         while ( true ) {
             if (tick> 0 )
             System.out.println( "--卖票--" +tick--);
         }
     }
}
public  class  TicketDemo {
     public  static  void  main(String args[]) {
         Ticket t1 = new  Ticket();
         Ticket t2 = new  Ticket();
         t1.start();
         t2.start();
     }
}

  Runnable接口实现卖票:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class  Ticket1 implements  Runnable {
     private  int  tick = 5 ;
     public  void  run(){
         while ( true ) {
             if (tick> 0 )
             System.out.println( "--卖票--" +tick--);
         }
     }
}
public  class  TicketRunnableDemo {
     public  static  void  main(String args[]) {
         Ticket1 t = new  Ticket1();
         Thread t1 = new  Thread(t);
         Thread t2 = new  Thread(t);
         t1.start();
         t2.start();
     }
}

  ★实现Runnable接口的Thread和MyThread ------代理设计模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
interface  Runnable {
     public  abstract  void  run();
}
 
class  Thread implements  Runnable {
     public  Thread(Runnable target) {
         
     }
     public  void  run() {
         //....
     }
     public  void  take() {
         System.out.println( "--代理实际模式--" );
     }
}
 
class  MyThread001 implements  Runnable {
     public  void  run() {
         //....
     }
}
 
public  class  BorrowsDemo {
     public  static  void  main(String args[]) {
         MyThread001 mt = new  MyThread001();
         Thread t = new  Thread(mt);
         t.take();
     }
}

  

---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值