首页 > 编程语言 > 详细

多线程之线程的实现

时间:2019-05-07 21:37:09      阅读:146      评论:0      收藏:0      [点我收藏+]

线程的实现方法有两种:

1.是继承Thread类,重写run方法。

2.是实现Runnable接口,实现run方法。

 

  继承Thread:

 1 public class Demo8 {
 2 
 3     public static void main(String[] args) {
 4         Mytext mytext=new Mytext();
 5         mytext.start();
 6 
 7     }    
 8 
 9 }
10 class Mytext extends Thread {
11     @Override
12     public void run() {
13         System.out.println("继承Thread");
14     }
15 }

 

  实现Runnable接口:

 1 public class Demo9 {
 2 
 3     public static void main(String[] args) {
 4         Mytext1 mytext1=new Mytext1();
 5         Thread thread=new Thread(mytext1);
 6         thread.start();
 7         
 8     }
 9 
10 }
11 class Mytext1 implements Runnable{
12 
13     @Override
14     public void run() {
15         System.out.println("实现Runnable接口");
16         
17     }
18     
19 }

 

多线程之线程的实现

原文:https://www.cnblogs.com/luojack/p/10828170.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!