传统线程的创建方式

前言:

  线程不是什么时髦的技术,但是是最基本的技术,项目做到最关键的时候主要就看这方面的知识功底是否扎实,它很有用,尽管它没有做一个小项目,深入一个框架来的快,但对于面试这是一个必问的项,同时也是对从事这一行长远发展有好处,是必备的基本功

什么是线程:一条执行线索(执行代码),多条代码同时执行的时候就是多线程,在没有知道线程之间,我们是无法同时执行两个线索的

传统的线程的创建:

 1 package com.xio.test;
 2 
 3 
 4 public class ThreadTest1 {
 5     
 6     public static void main(String[] args) {
 7         fun02();
 8     }
 9     
10     /*
11      * 线程也是一个东西。翻译成java语言就是一个对象
12      * 步骤:
13      * 1.得到一个线程对象,开辟一个线程
14      * 2.开启线程
15      * 3.加入你要执行的代码,线程是可以为空的也就意味着这条线程没有跑任何线索
16      * 
17      * 
18      * 创建方式:
19      * fun01:Thread thread1 = new Thread(){run}; thread1.start();
20      * fun02:Thread thread2 = new Thread(new runnanle(){run}) thread2.start();
21      */
22     public static void fun01() {
23         Thread thread1 = new Thread(){
24             /*父类中的方法:
25              *     @Override public void run() { 
26              *       if (target != null) { 
27              *            target.run(); 
28              *       } 
29              *     }
30              *  父类中的run方法在没有传进target对象的时候是没有执行任何代码
31              *  我们无法更改源码,但是可以通过new一个thread的子类去重写(覆盖)父类的方法
32              *  所以我们可以之间创建thread子类,目的是为了覆盖父类的方法,但是本质上还是thread起作用
33              */
34             @Override
35             public void run() {
36                 //让代码多执行一会儿
37                 while (true) {
38                     try {
39                         //让CPU休息0.5s再次执行
40                         Thread.sleep(500);
41                     } catch (InterruptedException e) {
42                         e.printStackTrace();
43                     }
44                     //得到当前这个线程对象的名字
45                     System.out.println("fun01:"+Thread.currentThread().getName());
46                 }
47             }
48             
49         };
50         thread1.start();
51     }
52 
53     public static void fun02() {
54         new Thread(new Runnable() {
55             public void run() {
56                 while (true) {
57                     try {
58                         Thread.sleep(500);
59                     } catch (InterruptedException e) {
60                         e.printStackTrace();
61                     }
62                     System.out.println("fun02:" + Thread.currentThread().getName());
63                 }
64             }
65         }).start();
66     }
67     /***
68      * 1.执行start方法
69      * 2.对象Thread的run(),有第2步不执行第3步,已经被第2步覆盖
70      * 3.传入的对象Runnable的run()
71      */
72     public static void fun03(){
73         new Thread(new Runnable() {
74             public void run() {
75                 while (true) {
76                     try {
77                         Thread.sleep(500);
78                     } catch (InterruptedException e) {
79                         e.printStackTrace();
80                     }
81                     System.out.println("runnable:" + Thread.currentThread().getName());
82                 }
83             }
84         }){
85             @Override
86             public void run() {
87                 while (true) {
88                     try {
89                         Thread.sleep(500);
90                     } catch (InterruptedException e) {
91                         e.printStackTrace();
92                     }
93                     System.out.println("thread:" + Thread.currentThread().getName());
94                 }
95             }
96         }.start();
97     }
98     
99 }

 

posted @ 2018-03-27 10:37  ❤月满西楼❤  阅读(185)  评论(0编辑  收藏  举报