线程是指运行中的应用程序,每个进程都有自己的独立的地址空间(内存空间),比如用户点击桌面的ie浏览器,就启动一个线程,操作系统就会为该进程分配独立的地址空间,启动多个浏览器就会创建多线程。
进程与线程关系
线程是线程中一个实体,是被系统独立调度和分配的基本单位,线程自己不拥有系统资源,只拥有一点在运行中必不可少的资源,但它可与同属一个进程的其他线程共享进程所拥有的全部资源。一个线程可以创建和撤销另一个线程,同一进程中的多个线程之间可以并发执行。线程有就绪、阻塞和运行三种基本状态。线程几种状态:
Ⅰ.新建状态(new)
Ⅱ.就绪状态(Runnable)
Ⅲ.运行状态(Running)
Ⅳ.阻塞状态(Blocked)
Ⅴ.死亡状态(Dead)
在Java中一个类要当作线程来死亡有两种方法:
Ⅰ.继承Thread类,并覆盖run函数
Ⅱ.实现Runnable接口,并覆盖run函数
实例
注意点:一个线程只能启动一次
通过继承Thread类实现线程
1 /* 2 * 作者:白客C 3 * 时间:2020年03月13日 4 * 内容:通过继承Thread类实现线程小案例 5 */ 6 7 package com.beekc.www; 8 9 public class Beekc{ 10 11 public static void main(String[] args) 12 { 13 //实例化一个对象 14 Cat cat = new Cat(); 15 //启动线程,run函数会自动运行 16 cat.start(); 17 } 18 19 } 20 21 class Cat extends Thread 22 { 23 //覆盖run函数 24 public void run() 25 { 26 while (true) 27 { 28 //休眠一秒 29 //用sleep让线程进行休眠 30 //Bolocked状态 31 try{ 32 Thread.sleep(1000); 33 }catch(Exception e){ 34 e.printStackTrace(); 35 } 36 37 System.out.println("hello world"); 38 } 39 } 40 }
通过继承Runnable接口实现线程
1 /* 2 * 作者:白客C 3 * 时间:2020年03月13日 4 * 内容:通过继承Runnable接口实现线程小案例 5 */ 6 7 package com.beekc.www; 8 9 public class Beekc{ 10 11 Dog dog = null; 12 Thread thread = null; 13 14 public static void main(String[] args) 15 { 16 Beekc beekc = new Beekc(); 17 } 18 19 public Beekc() 20 { 21 //注意线程的启动 22 dog = new Dog(); 23 //创建一个线程 24 thread = new Thread(dog); 25 thread.start(); 26 27 } 28 29 } 30 31 class Dog implements Runnable 32 { 33 //覆盖run方法 34 public void run() 35 { 36 while (true) 37 { 38 //休眠一秒 39 try{ 40 Thread.sleep(1000); 41 }catch(Exception e) 42 { 43 e.printStackTrace(); 44 } 45 46 System.out.println("hello, world"); 47 } 48 } 49 }
两个线程同时运行
1 /* 2 * 作者:白客C 3 * 时间:2020年03月13日 4 * 内容:两个线程同时运行小案例 5 */ 6 7 package com.beekc.www; 8 9 public class Beekc{ 10 11 Bird bird = null; 12 Pig pig = null; 13 Thread thread1 = null; 14 Thread thread2 = null; 15 16 public static void main(String[] args) 17 { 18 Beekc beekc =new Beekc(); 19 } 20 21 public Beekc() 22 { 23 bird = new Bird(10); 24 thread1 = new Thread(bird); 25 thread1.start(); 26 pig =new Pig(); 27 thread2 = new Thread(pig); 28 thread2.start(); 29 } 30 31 } 32 33 //打印 34 class Pig implements Runnable 35 { 36 int i = 0 ; 37 //覆盖run 38 public void run() 39 { 40 while (true) 41 { 42 //休眠一秒 43 try{ 44 Thread.sleep(1000); 45 }catch(Exception e){ 46 e.printStackTrace(); 47 } 48 i++; 49 System.out.println("打印第" + i + "条"); 50 51 if(i == 10) 52 { 53 break; 54 } 55 } 56 } 57 } 58 59 //算术 60 class Bird implements Runnable 61 { 62 int n; 63 int res = 0; 64 65 public Bird(int n) 66 { 67 this.n = n; 68 } 69 //覆盖run 70 public void run() 71 { 72 while(true) 73 { 74 //休眠一秒 75 try{ 76 Thread.sleep(1000); 77 }catch(Exception e) 78 { 79 e.printStackTrace(); 80 } 81 82 res += n; 83 System.out.println("rse = " + res); 84 n--; 85 if (n == 0) 86 { 87 System.out.println("总结果: " + res); 88 break; 89 } 90 91 } 92 } 93 }
Thread类 与 Runnable接口区别
通过继承Thread 类或者Runnable接口来创建线程本质上没有区别,从jdk帮助文档我们可以看到Thread类本身就实现了Runnable接口,如果一定要说他们有什么区别我在这里总结几点:
Ⅰ.尽可能使用实现Runnable接口的方式来创建线程
Ⅱ.在使用Thread的时候只需要new一个实例出来,调用start()方法即可以启动一个线程
Thread thread = new Thread();
thread.start();
Ⅲ.在使用Runnable的时候需要先new一个实现的Runnable的实例,之后用Thread调用
Text implements Runnable
Text text = new Text();
Thread thread = new Thread(text);
原文:https://www.cnblogs.com/beekc/p/12486796.html