带你聊聊 Java 并发编程之线程基础

开发 后端
百丈高楼平地起,要想学好多线程,首先还是的了解一下线程的基础,这边文章将带着大家来了解一下线程的基础知识。

 01、简介

百丈高楼平地起,要想学好多线程,首先还是的了解一下线程的基础,这边文章将带着大家来了解一下线程的基础知识。

[[281525]]

02、线程的创建方式

  1. 实现 Runnable 接口
  2. 继承 Thread 类
  3. 实现 Callable 接口通过 FutureTask 包装器来创建线程
  4. 通过线程池创建线程

下面将用线程池和 Callable 的方式来创建线程

  1. public class CallableDemo implements Callable<String> { 
  2.  
  3.     @Override 
  4.     public String call() throws Exception { 
  5.         int a=1; 
  6.         int b=2; 
  7.         System. out .println(a+b); 
  8.         return "执行结果:"+(a+b); 
  9.     } 
  10.  
  11.     public static void main(String[] args) throws ExecutionException, InterruptedException { 
  12.         //创建一个可重用固定线程数为1的线程池 
  13.         ExecutorService executorService = Executors.newFixedThreadPool (1); 
  14.         CallableDemo callableDemo=new CallableDemo(); 
  15.         //执行线程,用future来接收线程的返回值 
  16.         Future<String> future = executorService.submit(callableDemo); 
  17.         //打印线程的返回值 
  18.         System. out .println(future.get()); 
  19.         executorService.shutdown(); 
  20.     } 

执行结果

  1. 执行结果:3 

03、线程的生命周期

  1. NEW:初始状态,线程被构建,但是还没有调用 start 方法。
  2. RUNNABLED:运行状态,JAVA 线程把操作系统中的就绪和运行两种状态统一称为“运行中”。调用线程的 start() 方法使线程进入就绪状态。
  3. BLOCKED:阻塞状态,表示线程进入等待状态,也就是线程因为某种原因放弃了 CPU 使用权。比如访问 synchronized 关键字修饰的方法,没有获得对象锁。
  4. Waiting :等待状态,比如调用 wait() 方法。
  5. TIME_WAITING:超时等待状态,超时以后自动返回。比如调用 sleep(long millis) 方法
  6. TERMINATED:终止状态,表示当前线程执行完毕。

看下源码:

  1. public enum State { 
  2.         NEW, 
  3.         RUNNABLE, 
  4.         BLOCKED, 
  5.         WAITING, 
  6.         TIMED_WAITING, 
  7.         TERMINATED; 

04、线程的优先级

  1. 线程的最小优先级:1
  2. 线程的最大优先级:10
  3. 线程的默认优先级:5
  4. 通过调用 getPriority() 和 setPriority(int newPriority) 方法来获得和设置线程的优先级

看下源码:

  1. /** 
  2.      * The minimum priority that a thread can have. 
  3.      */ 
  4.     public final static int MIN_PRIORITY = 1; 
  5.  
  6.     /** 
  7.      * The default priority that is assigned to a thread. 
  8.      */ 
  9.     public final static int NORM_PRIORITY = 5; 
  10.  
  11.     /** 
  12.      * The maximum priority that a thread can have. 
  13.      */ 
  14.     public final static int MAX_PRIORITY = 10; 

看下代码:

  1. public class ThreadA extends Thread { 
  2.  
  3.     public static void main(String[] args) { 
  4.         ThreadA a = new ThreadA(); 
  5.         System.out.println(a.getPriority());//5 
  6.         a.setPriority(8); 
  7.         System.out.println(a.getPriority());//8 
  8.     } 

线程优先级特性:

  1. 继承性:比如 A 线程启动 B 线程,则B线程的优先级与 A 是一样的。
  2. 规则性:高优先级的线程总是大部分先执行完,但不代表高优先级线程全部先执行完。
  3. 随机性:优先级较高的线程不一定每一次都先执行完。

05、线程的停止

  1. stop() 方法,这个方法已经标记为过时了,强制停止线程,相当于 kill -9。
  2. interrupt() 方法,优雅的停止线程。告诉线程可以停止了,至于线程什么时候停止,取决于线程自身。

看下停止线程的代码:

  1. public class InterruptDemo { 
  2.     private static int i ; 
  3.     public static void main(String[] args) throws InterruptedException { 
  4.         Thread thread = new Thread(() -> { 
  5.             //默认情况下isInterrupted 返回 false、通过 thread.interrupt 变成了 true 
  6.             while (!Thread.currentThread().isInterrupted()) { 
  7.                 i++; 
  8.             } 
  9.             System.out.println("Num:" + i); 
  10.         }, "interruptDemo"); 
  11.         thread.start(); 
  12.         TimeUnit.SECONDS.sleep(1); 
  13.         thread.interrupt(); //不加这句,thread线程不会停止 
  14.     } 

看上面这段代码,主线程 main 方法调用 thread线程的 interrupt() 方法,就是告诉 thread 线程,你可以停止了(其实是将 thread 线程的一个属性设置为了 true ),然后 thread 线程通过 isInterrupted() 方法获取这个属性来判断是否设置为了 true。这里我再举一个例子来说明一下,

看代码:

  1. public class ThreadDemo { 
  2.     private volatile static Boolean interrupt = false ; 
  3.     private static int i ; 
  4.  
  5.     public static void main(String[] args) throws InterruptedException { 
  6.         Thread thread = new Thread(() -> { 
  7.             while (!interrupt) { 
  8.                 i++; 
  9.             } 
  10.             System.out.println("Num:" + i); 
  11.         }, "ThreadDemo"); 
  12.         thread.start(); 
  13.         TimeUnit.SECONDS.sleep(1); 
  14.         interrupt = true
  15.     } 

是不是很相似,再简单总结一下:

当其他线程通过调用当前线程的 interrupt 方法,表示向当前线程打个招呼,告诉他可以中断线程的执行了,并不会立即中断线程,至于什么时候中断,取决于当前线程自己。

线程通过检查自身是否被中断来进行相应,可以通过 isInterrupted() 来判断是否被中断。

这种通过标识符来实现中断操作的方式能够使线程在终止时有机会去清理资源,而不是武断地将线程停止,因此这种终止线程的做法显得更加安全和优雅。

06、线程的复位

两种复位方式:

  1. Thread.interrupted()
  2. 通过抛出 InterruptedException 的方式

然后了解一下什么是复位:

线程运行状态时 Thread.isInterrupted() 返回的线程状态是 false,然后调用 thread.interrupt() 中断线程 Thread.isInterrupted() 返回的线程状态是 true,最后调用 Thread.interrupted() 复位线程Thread.isInterrupted() 返回的线程状态是 false 或者抛出 InterruptedException 异常之前,线程会将状态设为 false。

下面来看下两种方式复位线程的代码,首先是 Thread.interrupted() 的方式复位代码:

  1. public class InterruptDemo { 
  2.  
  3.     public static void main(String[] args) throws InterruptedException { 
  4.         Thread thread = new Thread(() -> { 
  5.             while (true) { 
  6.                 //Thread.currentThread().isInterrupted()默认是false,当main方式执行thread.interrupt()时,状态改为true 
  7.                 if (Thread.currentThread().isInterrupted()) { 
  8.                     System.out.println("before:" + Thread.currentThread().isInterrupted());//before:true 
  9.                     Thread.interrupted(); // 对线程进行复位,由 true 变成 false 
  10.                     System.out.println("after:" + Thread.currentThread().isInterrupted());//after:false 
  11.                 } 
  12.             } 
  13.         }, "interruptDemo"); 
  14.         thread.start(); 
  15.         TimeUnit.SECONDS.sleep(1); 
  16.         thread.interrupt(); 
  17.     } 

抛出 InterruptedException 复位线程代码:

  1. public class InterruptedExceptionDemo { 
  2.  
  3.     public static void main(String[] args) throws InterruptedException { 
  4.         Thread thread = new Thread(() -> { 
  5.             while (!Thread.currentThread().isInterrupted()) { 
  6.                 try { 
  7.                     TimeUnit.SECONDS.sleep(1); 
  8.                 } catch (InterruptedException e) { 
  9.                     e.printStackTrace(); 
  10.                     // break; 
  11.                 } 
  12.             } 
  13.         }, "interruptDemo"); 
  14.         thread.start(); 
  15.         TimeUnit.SECONDS.sleep(1); 
  16.         thread.interrupt(); 
  17.         System.out.println(thread.isInterrupted()); 
  18.     } 

结果:

  1. false 
  2. java.lang.InterruptedException: sleep interrupted 
  3.     at java.lang.Thread.sleep(Native Method) 
  4.     at java.lang.Thread.sleep(Thread.java:340) 
  5.     at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) 
  6.     at com.cl.concurrentprogram.InterruptedExceptionDemo.lambda$main$0(InterruptedExceptionDemo.java:16) 
  7.     at java.lang.Thread.run(Thread.java:748) 

需要注意的是,InterruptedException 异常的抛出并不意味着线程必须终止,而是提醒当前线程有中断的操作发生,至于接下来怎么处理取决于线程本身,比如

  1. 直接捕获异常不做任何处理
  2. 将异常往外抛出
  3. 停止当前线程,并打印异常信息

像我上面的例子,如果抛出 InterruptedException 异常,我就break跳出循环让 thread 线程终止。

为什么要复位:

Thread.interrupted() 是属于当前线程的,是当前线程对外界中断信号的一个响应,表示自己已经得到了中断信号,但不会立刻中断自己,具体什么时候中断由自己决定,让外界知道在自身中断前,他的中断状态仍然是 false,这就是复位的原因。

责任编辑:华轩 来源: Java极客技术
相关推荐

2017-09-19 14:53:37

Java并发编程并发代码设计

2017-01-10 13:39:57

Python线程池进程池

2020-12-08 08:53:53

编程ThreadPoolE线程池

2023-07-11 08:34:25

参数流程类型

2011-07-21 10:17:53

java

2023-07-03 09:59:00

并发编程并发容器

2012-03-09 10:44:11

Java

2011-12-29 13:31:15

Java

2021-03-10 15:59:39

JavaSynchronize并发编程

2020-12-10 07:00:38

编程线程池定时任务

2023-04-02 17:53:10

多线程编程自测

2024-03-12 13:11:20

powerjob单机线程

2020-11-13 08:42:24

Synchronize

2020-12-11 07:32:45

编程ThreadLocalJava

2020-12-03 11:15:21

CyclicBarri

2020-12-04 19:28:53

CountDownLaPhaserCyclicBarri

2020-12-09 08:21:47

编程Exchanger工具

2020-11-30 16:01:03

Semaphore

2020-12-16 10:54:52

编程ForkJoin框架

2020-07-06 08:03:32

Java悲观锁乐观锁
点赞
收藏

51CTO技术栈公众号