标签:thread margin port 基础 mina focus void 语言 ott
在java多线程并发编程中,有八大基础核心。考考你:
看看都有哪八大基础核心呢?它们分别是:
1.创建线程的方式
2.线程启动
3.线程停止
4.线程生命周期
5.线程相关的方法
6.线程相关的属性
7.线程异常处理
8.线程安全
今天我们从第四个基础核心开始:线程生命周期
#前情回顾:
在java编程语言中,从线程创建,到线程执行结束,会经过一系列状态的转化,称为线程的生命周期
#考考你:
1.你知道线程生命周期中有哪些状态吗?
2.你知道各种状态对应的含义吗?
1 package com.anan.thread.threadstate; 2 3 import java.util.concurrent.TimeUnit; 4 5 /** 6 * 演示线程状态:NEW/RUNNABLE/TERMINATED 7 */ 8 public class ThreadStateDemo1 { 9 10 public static void main(String[] args) throws InterruptedException{ 11 // 创建线程对象 12 String tName = "my-thread"; 13 Runnable r1 = new MyRunnable(); 14 Thread t1 = new Thread(r1,tName); 15 16 // 输出线程状态:NEW 17 System.out.println("1.新建线程:" + tName + "当前状态:" + t1.getState()); 18 19 // 启动线程,等待1毫秒,输出线程状态:RUNNABLE 20 t1.start(); 21 TimeUnit.MILLISECONDS.sleep(1); 22 System.out.println("2.启动线程后:" + tName + "当前状态:" + t1.getState()); 23 24 // 发送中断信号,等待1毫秒,输出线程状态:TERMINATED 25 t1.interrupt(); 26 TimeUnit.MILLISECONDS.sleep(1); 27 System.out.println("3.给线程发送中断信号后:" + tName + "当前状态:" + t1.getState()); 28 29 } 30 } 31 32 /** 33 * 实现Runnable接口,创建线程 34 */ 35 class MyRunnable implements Runnable{ 36 public void run() { 37 while (!Thread.currentThread().isInterrupted()){ 38 ;// 不断循环,等待中断信号发生,然后结束线程运行 39 } 40 System.out.println("中断信号发生," + Thread.currentThread().getName() + "准备结束运行."); 41 } 42 }
通过该案例,演示线程的BLOCKED状态。业务描述:
1.模拟获取共享资源银行:Bank账户余额信息,在获取账户余额时,需要加上同步锁
2.创建两个线程,并发获取银行账户余额,模拟当一个线程加锁操作中,另外一个线程只能阻塞等待
3.在主线程中,输出两个线程的状态
1 package com.anan.thread.threadstate; 2 3 import java.util.concurrent.TimeUnit; 4 5 /** 6 * 演示线程状态:BLOCKED 7 */ 8 public class ThreadStateBlockedDemo { 9 // 公共锁对象 10 public static final Object LOCK = new Object(); 11 12 /** 13 * 2.创建两个线程,并发获取银行账户余额, 14 * 模拟当一个线程加锁操作中,另外一个线程只能阻塞等待 15 */ 16 public static void main(String[] args) { 17 18 // 创建Runnable对象 19 Runnable r1 = new MyRunnable1(); 20 21 // 创建两个线程对象 22 String tName_1 = "my-thread-1"; 23 Thread t1 = new Thread(r1,tName_1); 24 25 String tName_2 = "my-thread-2"; 26 Thread t2 = new Thread(r1,tName_2); 27 28 // 启动线程t1,t2 29 t1.start(); 30 t2.start(); 31 32 // 输出两个线程:t1,t2当前状态 33 System.out.println("1.主线程"+ Thread.currentThread().getName() + 34 "打印,线程:" + t1.getName() + "当前状态:" + t1.getState()); 35 System.out.println("2.主线程"+ Thread.currentThread().getName() + 36 "打印,线程:" + t2.getName() + "当前状态:" + t2.getState()); 37 38 39 } 40 41 /** 42 * 1.模拟获取共享资源银行:Bank 43 * 账户余额信息,在获取账户余额时,需要加上同步锁 44 */ 45 public static void getBankMoney() { 46 synchronized (LOCK){ 47 System.out.println(Thread.currentThread().getName() + "线程,获取到锁###."); 48 // 休眠1秒,模拟业务操作 49 try { 50 TimeUnit.SECONDS.sleep(1); 51 // 打印输出账户余额 52 System.out.println("线程:" + Thread.currentThread().getName() + 53 "获取到账户余额了......"); 54 } catch (InterruptedException e) { 55 e.printStackTrace(); 56 } 57 } 58 59 System.out.println(Thread.currentThread().getName() + "线程,释放锁###."); 60 } 61 62 } 63 64 /** 65 * 实现Runnable接口,创建线程 66 */ 67 class MyRunnable1 implements Runnable{ 68 69 public void run() { 70 // 获取账户余额 71 ThreadStateBlockedDemo.getBankMoney(); 72 } 73 }
1 package com.anan.thread.threadstate; 2 3 import java.util.concurrent.TimeUnit; 4 5 /** 6 * 演示线程状态:WAITING 7 */ 8 public class ThreadStateWaitingDemo { 9 10 // 创建公共锁 11 public final static Object LOCK = new Object(); 12 13 public static void main(String[] args) throws InterruptedException{ 14 // 创建线程对象 15 Runnable r1 = new MyRunnable3(); 16 String tName_1 = "my-thread-1"; 17 Thread t1 = new Thread(r1,tName_1); 18 19 // 启动线程,休眠1秒后,获取线程状态 20 t1.start(); 21 TimeUnit.SECONDS.sleep(1); 22 System.out.println("1.主线程"+ Thread.currentThread().getName() + 23 "打印,线程:" + t1.getName() + "当前状态:" + t1.getState()); 24 25 // 主线程唤醒t1线程,再次输出线程状态 26 synchronized (LOCK){ 27 LOCK.notify(); 28 } 29 TimeUnit.SECONDS.sleep(1); 30 System.out.println("2.主线程"+ Thread.currentThread().getName() + 31 "打印,线程:" + t1.getName() + "当前状态:" + t1.getState()); 32 } 33 } 34 35 /** 36 * 实现Runnable接口,创建线程 37 */ 38 class MyRunnable3 implements Runnable{ 39 40 public void run() { 41 System.out.println("线程:" + Thread.currentThread().getName() + 42 "即将进入等待:ThreadStateWaitingDemo.LOCK.wait(),等待主线程输出状态."); 43 synchronized (ThreadStateWaitingDemo.LOCK){ 44 try { 45 ThreadStateWaitingDemo.LOCK.wait(); 46 } catch (InterruptedException e) { 47 e.printStackTrace(); 48 } 49 } 50 System.out.println("线程:" + Thread.currentThread().getName() + 51 "被唤醒执行结束,等待主线程输出状态."); 52 } 53 }
1 package com.anan.thread.threadstate; 2 3 import java.util.concurrent.TimeUnit; 4 5 /** 6 * 演示线程状态:TIMED_WAITING 7 */ 8 public class ThreadStateTimedWaitingDemo { 9 10 public static void main(String[] args) throws InterruptedException{ 11 12 // 创建线程对象 13 Runnable r1 = new MyRunnable2(); 14 String tName_1 = "my-thread-1"; 15 Thread t1 = new Thread(r1,tName_1); 16 17 // 启动线程,休眠1秒后,获取线程状态 18 t1.start(); 19 TimeUnit.SECONDS.sleep(1); 20 System.out.println("1.主线程"+ Thread.currentThread().getName() + 21 "打印,线程:" + t1.getName() + "当前状态:" + t1.getState()); 22 23 } 24 } 25 26 /** 27 * 实现Runnable接口,创建线程 28 */ 29 class MyRunnable2 implements Runnable{ 30 31 public void run() { 32 System.out.println("线程:" + Thread.currentThread().getName() + 33 "准备休眠3秒:TimeUnit.SECONDS.sleep(3),等待主线程输出状态."); 34 try { 35 TimeUnit.SECONDS.sleep(3); 36 } catch (InterruptedException e) { 37 e.printStackTrace(); 38 } 39 } 40 }
#考考你答案:
1.你知道线程生命周期中有哪些状态吗?
1.1.在java编程语言中,线程的生命周期总共有6种状态
2.你知道各种状态对应的含义吗?
2.1.分别是:
新建:NEW
可运行:RUNNABLE
已终止:TERMINATED
阻塞:BLOCKED
等待:WAITING
计时等待:TIMED_WAITING
标签:thread margin port 基础 mina focus void 语言 ott
原文地址:https://www.cnblogs.com/itall/p/12268060.html