- 进程
- 进程是程序的一次动态执行过程,它需要经历从代码加载,代码执行、执行完毕、最终消亡的过程。
- 线程
- 多线程
Java中线程实现的方式
| > 在 Java 中实现多线程有两种手段,一种是继承 Thread 类,另一种就是实现 Runnable 接口。下面我们就分别来介绍这两种方式的使用。 |
- 实现 Runnable 接口
| package Thread; |
| |
| public class MyThread implements Runnable { |
| |
| private String name; |
| |
| MyThread(String name) { |
| this.name = name; |
| } |
| |
| |
| @Override |
| public void run() { |
| for (int i = 0; i < 4; i++) { |
| System.out.println(name + "运行,i = " + i); |
| } |
| } |
| |
| public static void main(String[] args) { |
| MyThread mt1 = new MyThread("线程A"); |
| MyThread mt2 = new MyThread("线程B"); |
| Thread t1 = new Thread(mt1); |
| Thread t2 = new Thread(mt2); |
| t1.start(); |
| t2.start(); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| } |
- 继承 Thread 类
| package Thread; |
| |
| public class MyThread extends Thread { |
| private String name; |
| |
| public MyThread(String name) { |
| this.name = name; |
| } |
| |
| public void run() { |
| for (int i = 0; i < 4; i++) { |
| System.out.println(name + "运行,i = " + i); |
| } |
| } |
| |
| public static void main(String[] args) { |
| MyThread mt1 = new MyThread("线程A "); |
| MyThread mt2 = new MyThread("线程B "); |
| mt1.start(); |
| mt2.start(); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| } |
- 取得和设置线程的名称
| public class MyThread implements Runnable { |
| |
| @Override |
| public void run() { |
| for (int i = 0; i < 4; i++) { |
| System.out.println(Thread.currentThread().getName() + "运行,第" + i + "次!"); |
| } |
| } |
| |
| public static void main(String[] args) { |
| MyThread myThread = new MyThread(); |
| new Thread(myThread).start(); |
| System.out.println("------------"); |
| new Thread(myThread, "线程A").start(); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| } |
- 运行结果
- 从程序可以看出,现在的两个线程对象是交错运行的,哪个线程对象抢到了 CPU 资源,哪个线程就可以运行,所以程序每次的运行结果肯定是不一样的。
- 在线程启动虽然调用的是 start() 方法,但实际上调用的却是 run() 方法定义的主体。
线程的状态变化
| > 要想实现多线程,必须在主线程中创建新的线程对象。任何线程一般具有5种状态,即创建,就绪,运行,阻塞,终止。下面分别介绍一下这几种状态: |
- 在此提出一个问题,Java 程序每次运行至少启动几个线程?
- 回答:至少启动两个线程,每当使用 Java 命令执行一个类时,实际上都会启动一个 JVM,每一个JVM实际上就是在操作系统中启动一个线程。
- Java 本身具备了垃圾的收集机制。
- 所以在 Java 运行时至少会启动两个线程,一个是 main 线程,另外一个是垃圾收集线程
- 创建状态
- 在程序中用构造方法创建了一个线程对象后,新的线程对象便处于新建状态,此时它已经有了相应的内存空间和其他资源,但还处于不可运行状态。
- 新建一个线程对象可采用Thread 类的构造方法来实现,例如 “Thread thread=new Thread()”。
- 就绪状态
- 新建线程对象后,调用该线程的 start() 方法就可以启动线程。
- 当线程启动时,线程进入就绪状态。
- 此时,线程将进入线程队列排队,等待 CPU 服务,这表明它已经具备了运行条件。
- 运行状态
- 当就绪状态被调用并获得处理器资源时,线程就进入了运行状态。
- 此时,自动调用该线程对象的 run() 方法。run() 方法定义该线程的操作和功能。
- 阻塞状态
- 一个正在执行的线程在某些特殊情况下,如被人为挂起或需要执行耗时的输入/输出操作,会让 CPU 暂时中止自己的执行,进入阻塞状态。
- 在可执行状态下,如果调用sleep(),suspend(),wait() 等方法,线程都将进入阻塞状态。
- 发生阻塞时线程不能进入排队队列,只有当引起阻塞的原因被消除后,线程才可以转入就绪状态。
- 死亡状态
- 线程调用 stop() 方法时或 run() 方法执行结束后,即处于死亡状态。
- 处于死亡状态的线程不具有继续运行的能力。
线程的操作方法
| > 刚才在分析自定义模式工作原理的时候其实就已经提到了,如果想要更改Glide的默认配 |
- 线程的强制运行
- 在线程操作中,可以使用 join() 方法让一个线程强制运行,线程强制运行期间,其他线程无法运行,必须等待此线程完成之后才可以继续执行。
| public class MyThread implements Runnable { |
| |
| @Override |
| public void run() { |
| for (int i = 0; i < 4; i++) { |
| System.out.println(Thread.currentThread().getName() + "运行,第" + i + "次!"); |
| } |
| } |
| |
| public static void main(String[] args) { |
| MyThread myThread = new MyThread(); |
| new Thread(myThread).start(); |
| System.out.println("------------"); |
| Thread threadA = new Thread(myThread, "线程A"); |
| threadA.start(); |
| for (int i = 0; i < 10; i++) { |
| if (i > 5) { |
| try { |
| threadA.join(); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| } |
| System.out.println("Main线程运行 --> " + i); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| } |
- 取得和设置线程的名称
| public class MyThread implements Runnable { |
| |
| @Override |
| public void run() { |
| for (int i = 0; i < 4; i++) { |
| System.out.println(Thread.currentThread().getName() + "运行,第" + i + "次!"); |
| } |
| } |
| |
| public static void main(String[] args) { |
| MyThread myThread = new MyThread(); |
| new Thread(myThread).start(); |
| System.out.println("------------"); |
| new Thread(myThread, "线程A").start(); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| } |
线程的操作方法
| > 刚才在分析自定义模式工作原理的时候其实就已经提到了,如果想要更改Glide的默认配 |
- 线程的强制运行
- 在线程操作中,可以使用 join() 方法让一个线程强制运行,线程强制运行期间,其他线程无法运行,必须等待此线程完成之后才可以继续执行。
| public class MyThread implements Runnable { |
| |
| @Override |
| public void run() { |
| for (int i = 0; i < 4; i++) { |
| System.out.println(Thread.currentThread().getName() + "运行,第" + i + "次!"); |
| } |
| } |
| |
| public static void main(String[] args) { |
| MyThread myThread = new MyThread(); |
| new Thread(myThread).start(); |
| System.out.println("------------"); |
| Thread threadA = new Thread(myThread, "线程A"); |
| threadA.start(); |
| for (int i = 0; i < 10; i++) { |
| if (i > 5) { |
| try { |
| threadA.join(); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| } |
| System.out.println("Main线程运行 --> " + i); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| } |
- 线程的休眠
- 在程序中允许一个线程进行暂时的休眠,直接使用 Thread.sleep() 即可实现休眠。
| public class MyThread implements Runnable { |
| |
| @Override |
| public void run() { |
| for (int i = 0; i < 4; i++) { |
| try { |
| Thread.sleep(500); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| System.out.println(Thread.currentThread().getName() + "运行,第" + i + "次!"); |
| } |
| } |
| |
| public static void main(String[] args) { |
| MyThread mt = new MyThread(); |
| Thread t = new Thread(mt, "线程"); |
| t.start(); |
| |
| |
| |
| |
| |
| |
| |
| } |
| } |
- 中断线程
- 当一个线程运行时,另外一个线程可以直接通过interrupt()方法中断其运行状态。
| public class MyThread implements Runnable { |
| |
| @Override |
| public void run() { |
| System.out.println("1、进入run()方法"); |
| try { |
| Thread.sleep(10000); |
| System.out.println("2、已经完成了休眠"); |
| } catch (InterruptedException e) { |
| System.out.println("3、休眠被终止02"); |
| return; |
| } |
| System.out.println("4、run()方法正常结束"); |
| } |
| |
| public static void main(String[] args) { |
| MyThread mt = new MyThread(); |
| Thread t = new Thread(mt, "线程"); |
| t.start(); |
| try { |
| Thread.sleep(2000); |
| } catch (InterruptedException e) { |
| System.out.println("3、休眠被终止01"); |
| } |
| t.interrupt(); |
| |
| |
| } |
| } |
- 后台线程
- 在 Java 程序中, 只要前台有一个线程在运行, 则整个 Java 进程都不会消失, 所以此时可以设置一个后台线程, 这样即使 Java 线程结束了, 此后台线程依然会继续执行, 要想实现这样的操作, 直接使用 setDaemon() 方法即可。
- 在线程类 MyThread 中,尽管 run() 方法中是死循环的方式,但是程序依然可以执行完,因为方法中死循环的线程操作已经设置成后台运行。
| public class MyThread implements Runnable { |
| |
| @Override |
| public void run() { |
| for (int i = 0; i < 4; i++) { |
| System.out.println(Thread.currentThread().getName() + "运行,第" + i + "次!"); |
| } |
| } |
| |
| public static void main(String[] args) { |
| MyThread mt = new MyThread(); |
| Thread t = new Thread(mt, "线程"); |
| t.setDaemon(true); |
| t.start(); |
| } |
| } |
- 线程的优先级
- 在 Java 的线程操作中, 所有的线程在运行前都会保持在就绪状态, 那么此时, 哪个线程的优先级高, 哪个线程就有可能会先被执行。
- 从程序的运行结果中可以观察到, 线程将根据其优先级的大小来决定哪个线程会先运行, 但是需要注意并非优先级越高就一定会先执行, 哪个线程先执行将由 CPU 的调度决定。
| public class MyThread implements Runnable { |
| |
| @Override |
| public void run() { |
| for (int i = 0; i < 4; i++) { |
| System.out.println(Thread.currentThread().getName() + "运行,第" + i + "次!"); |
| } |
| } |
| |
| public static void main(String[] args) { |
| MyThread mt01 = new MyThread(); |
| MyThread mt02 = new MyThread(); |
| MyThread mt03 = new MyThread(); |
| Thread t01 = new Thread(mt01, "线程A"); |
| Thread t02 = new Thread(mt02, "线程B"); |
| Thread t03 = new Thread(mt03, "线程C"); |
| |
| |
| t01.setPriority(Thread.MAX_PRIORITY); |
| t02.setPriority(Thread.NORM_PRIORITY); |
| t03.setPriority(Thread.MIN_PRIORITY); |
| |
| t01.start(); |
| t02.start(); |
| t03.start(); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| } |
- 线程的礼让
- 在线程操作中,也可以使用 yield() 方法将一个线程的操作暂时让给其他线程执行
| public class MyThread implements Runnable { |
| |
| @Override |
| public void run() { |
| for (int i = 0; i < 4; i++) { |
| try { |
| Thread.sleep(500); |
| } catch (Exception e) { |
| } |
| System.out.println(Thread.currentThread().getName() + "运行,第" + i + "次!"); |
| if (i == 2) { |
| System.out.print("线程礼让: "); |
| Thread.currentThread().yield(); |
| } |
| } |
| } |
| |
| public static void main(String args[]) { |
| MyThread my = new MyThread(); |
| Thread t1 = new Thread(my, "线程A"); |
| Thread t2 = new Thread(my, "线程B"); |
| t1.start(); |
| t2.start(); |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| } |
同步以及死锁
- 一个多线程的程序如果是通过 Runnable 接口实现的,则意味着类中的属性被多个线程共享,那么这样就会造成一种问题,如果这多个线程要操作同一个资源时就有可能出现资源同步问题。
| synchronized(同步对象){ |
| 需要同步的代码 |
| } |
| |
| class MyThread implements Runnable{ |
| private int ticket = 5 ; |
| public void run(){ |
| for(int i=0;i<100;i++){ |
| synchronized(this){ |
| if(ticket>0){ |
| try{ |
| Thread.sleep(300) ; |
| }catch(InterruptedException e){ |
| e.printStackTrace() ; |
| } |
| System.out.println("卖票:ticket = " + ticket-- ); |
| } |
| } |
| } |
| } |
| }; |
| public class SyncDemo02{ |
| public static void main(String args[]){ |
| MyThread mt = new MyThread() ; |
| Thread t1 = new Thread(mt) ; |
| Thread t2 = new Thread(mt) ; |
| Thread t3 = new Thread(mt) ; |
| t1.start() ; |
| t2.start() ; |
| t3.start() ; |
| |
| |
| |
| |
| |
| |
| } |
| }; |
- 同步方法
- 除了可以将需要的代码设置成同步代码块外,也可以使用 synchronized 关键字将一个方法声明为同步方法。
- 从程序运行的结果可以发现,此代码完成了与之前同步代码同样的功能。
| synchronized 方法返回值 方法名称(参数列表){} |
| |
| class MyThread03 implements Runnable { |
| private int ticket = 5; |
| |
| public void run() { |
| for (int i = 0; i < 100; i++) { |
| this.sale(); |
| } |
| } |
| |
| public synchronized void sale() { |
| if (ticket > 0) { |
| try { |
| Thread.sleep(300); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| System.out.println("卖票:ticket = " + ticket--); |
| } |
| |
| } |
| }; |
| |
| public class SyncDemo03 { |
| public static void main(String args[]) { |
| MyThread03 mt = new MyThread03(); |
| Thread t1 = new Thread(mt); |
| Thread t2 = new Thread(mt); |
| Thread t3 = new Thread(mt); |
| t1.start(); |
| t2.start(); |
| t3.start(); |
| |
| |
| |
| |
| |
| |
| } |
| }; |
- 死锁
- 同步可以保证资源共享操作的正确性,但是过多同步也会产生问题。例如,现在张三想要李四的画,李四想要张三的书,张三对李四说“把你的画给我,我就给你书”,李四也对张三说“把你的书给我,我就给你画”两个人互相等对方先行动,就这么干等没有结果,这实际上就是死锁的概念。
- 所谓死锁,就是两个线程都在等待对方先完成,造成程序的停滞,一般程序的死锁都是在程序运行时出现的。
- 下面以一个简单范例说明这个概念, 以下代码不再执行,程序进入死锁状态。
| class Zhangsan { |
| public void say() { |
| System.out.println("张三对李四说:“你给我画,我就把书给你。”"); |
| } |
| |
| public void get() { |
| System.out.println("张三得到画了。"); |
| } |
| }; |
| |
| class Lisi { |
| public void say() { |
| System.out.println("李四对张三说:“你给我书,我就把画给你”"); |
| } |
| |
| public void get() { |
| System.out.println("李四得到书了。"); |
| } |
| }; |
| |
| public class ThreadDeadLock implements Runnable { |
| private static Zhangsan zs = new Zhangsan(); |
| private static Lisi ls = new Lisi(); |
| private boolean flag = false; |
| |
| public void run() { |
| if (flag) { |
| synchronized (zs) { |
| zs.say(); |
| try { |
| Thread.sleep(500); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| synchronized (ls) { |
| zs.get(); |
| } |
| } |
| } else { |
| synchronized (ls) { |
| ls.say(); |
| try { |
| Thread.sleep(500); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| synchronized (zs) { |
| ls.get(); |
| } |
| } |
| } |
| } |
| |
| public static void main(String args[]) { |
| ThreadDeadLock t1 = new ThreadDeadLock(); |
| ThreadDeadLock t2 = new ThreadDeadLock(); |
| t1.flag = true; |
| t2.flag = false; |
| Thread thA = new Thread(t1); |
| Thread thB = new Thread(t2); |
| thA.start(); |
| thB.start(); |
| |
| |
| } |
| }; |
线程的启动方式
| public class Test { |
| public static void main(String[] args) { |
| |
| Thread magotan = new Thread(new Runnable() { |
| |
| @Override |
| public void run() { |
| System.out.println("MAGOTAN"); |
| } |
| }); |
| |
| |
| Thread passat = new Thread(() -> System.out.println("PASSAT")); |
| |
| |
| magotan.run(); |
| |
| passat.run(); |
| } |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~