(一)创建传统线程的两种方式

 

 

 1 package cn.itcast.heima2;
 2 
 3 /**
 4  * 传统创建线程的两种方式
 5  * 1. new Thread子类重写run()方法
 6  * 2. new Runnable()(最常采用的方式,因为这种方式更加体现面向对象的编程)
 7  * Thread线程对象,线程要运行代码,所有代码封装到Runnable线程锁中,两个对象一组合,更加体现面向对象编程 
 8  */
 9 public class TraditionalThread {
10 
11     public static void main(String[] args) {
12         //第一个线程
13         Thread thread = new Thread() {
14 
15             @Override
16             public void run() {
17                 while (true) {
18                     try {
19                         //导致当前线程的睡眠时间(毫秒数),但是当前线程不会失去任何监视器的所有权
20                         Thread.sleep(500);
21                     } catch (InterruptedException e) {
22                         e.printStackTrace();
23                     }
24                     //currentThread对当前正在执行的线程对象的引用
25                     System.out.println("第一个" + Thread.currentThread().getName());
26                     System.out.println("第二个" + this.getName());
27                 }
28             }
29         };
30         thread.start();
31         //第二个线程
32         Thread thread2 = new Thread(new Runnable() {
33 
34             @Override
35             public void run() {
36                 while (true) {
37                     try {
38                         //导致当前线程的睡眠时间(毫秒数),但是当前线程不会失去任何监视器的所有权
39                         Thread.sleep(500);
40                     } catch (InterruptedException e) {
41                         e.printStackTrace();
42                     }
43                     //currentThread对当前正在执行的线程对象的引用
44                     System.out.println("第三个" + Thread.currentThread().getName());
45                 }
46             }
47         });
48         thread2.start();
49         //第三个线程
50         //对象结构:new Thread(runnable.run)(run).start();
51         //思考:该对象打印的是runnable还是thread
52         new Thread(
53                 new Runnable() {
54 
55                     public void run() {
56                         while (true) {
57                             try {
58                                 //导致当前线程的睡眠时间(毫秒数),但是当前线程不会失去任何监视器的所有权
59                                 Thread.sleep(500);
60                             } catch (InterruptedException e) {
61                                 e.printStackTrace();
62                             }
63                             //currentThread对当前正在执行的线程对象的引用
64                             System.out.println("runable" + Thread.currentThread().getName());
65                         }
66                     };
67                 }) {
68 
69             public void run() {
70                 while (true) {
71                     try {
72                         //导致当前线程的睡眠时间(毫秒数),但是当前线程不会失去任何监视器的所有权
73                         Thread.sleep(500);
74                     } catch (InterruptedException e) {
75                         e.printStackTrace();
76                     }
77                     //currentThread对当前正在执行的线程对象的引用
78                     System.out.println("thread:" + Thread.currentThread().getName());
79                 }
80             }
81         }.start();
82     }
83 }

 

posted @ 2017-05-17 00:55  李梦晨  阅读(271)  评论(0编辑  收藏  举报