JAVA并发编程3--多线程程序

1.创建线程的方法:

案例:计算1-1000的整数和

实现Runnable接口

步骤:
1.创建一个实现了Runnable接口的类
2.实现类去实现Runnable中的抽象方法:run()
3.创建实现类的对象
4.将此对象作为参数传递到Thread类的构造器中,创建Thread类的对象
5.通过Thread类的对象调用start() ① 启动线程 ②调用当前线程的run()–>调用了Runnable类型的target的run()

import java.util.concurrent.atomic.AtomicLong;public class ThreadLearn {// 使用 AtomicLong 替代 Long,保证线程安全且避免空指针问题public static AtomicLong result = new AtomicLong(0);static int count = 100000;public static CountDownLatch countDownLatch = new CountDownLatch(count);public static ExecutorService executor=Executors.newFixedThreadPool(10);public static void main(String[] args) throws InterruptedException, ExecutionException {long start = System.currentTimeMillis();Thread[] threads = new Thread[count];for (int i = 0; i < count; i++) {MyThread myThread = new MyThread(100);threads[i] = new Thread(myThread);threads[i].start();}countDownLatch.await();System.out.println(result.get()); // 使用 get 方法获取 AtomicLong 的值System.out.println(System.currentTimeMillis() - start);}
}
class MyThread implements Runnable {private int count;public MyThread(int count) {this.count = count;}@Overridepublic void run() {int sum = 0;for (int i = 0; i <= count; i++) {sum += i;}// 使用 AtomicLong 的 addAndGet 方法进行原子更新ThreadLearn.result.addAndGet(sum);ThreadLearn.countDownLatch.countDown();}
}

继承Thread类

步骤:
1.创建一个继承于Thread类的子类
2.重写Thread类的run() --> 将此线程执行的操作声明在run()中
3.创建Thread类的子类的对象
4.通过此对象调用start()执行线程

import java.util.concurrent.atomic.AtomicLong;public class ThreadLearn {// 使用 AtomicLong 替代 Long,保证线程安全且避免空指针问题public static AtomicLong result = new AtomicLong(0);static int count = 100000;public static CountDownLatch countDownLatch = new CountDownLatch(count);public static ExecutorService executor=Executors.newFixedThreadPool(10);public static void main(String[] args) throws InterruptedException, ExecutionException {long start = System.currentTimeMillis();Thread[] threads = new Thread[count];for (int i = 0; i < count; i++) {MyThread1 myThread = new MyThread1(100);threads[i] = new Thread(myThread);threads[i].start();}countDownLatch.await();System.out.println(result.get()); // 使用 get 方法获取 AtomicLong 的值System.out.println(System.currentTimeMillis() - start);}
}
class MyThread1 extends Thread {private long count;MyThread1(int count) {this.count = count;}@Overridepublic void run() {int sum = 0;for (int i = 0; i <= count; i++) {sum += i;}// 使用 AtomicLong 的 addAndGet 方法进行原子更新ThreadLearn.result.addAndGet(sum);ThreadLearn.countDownLatch.countDown();}
}

实现Callable接口

步骤:
1.创建一个实现Callable的实现类
2.实现call方法,将此线程需要执行的操作声明在call()中
3.创建Callable接口实现类的对象
4.将此Callable接口实现类的对象作为传递到FutureTask构造器中,创建FutureTask的对象
5.将FutureTask的对象作为参数传递到Thread类的构造器中,创建Thread对象,并调用start()
6.获取Callable中call方法的返回值

package Reflection;import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.atomic.AtomicLong;public class ThreadLearn {// 使用 AtomicLong 替代 Long,保证线程安全且避免空指针问题public static AtomicLong result = new AtomicLong(0);static int count = 100000;public static CountDownLatch countDownLatch = new CountDownLatch(count);public static void main(String[] args) throws InterruptedException, ExecutionException {
//        long start = System.currentTimeMillis();
//        Thread[] threads = new Thread[count];
//        for (int i = 0; i < count; i++) {
//            MyThread1 myThread = new MyThread1(100);
//            threads[i] = new Thread(myThread);
//            threads[i].start();
//        }
//        countDownLatch.await();
//        System.out.println(result.get()); // 使用 get 方法获取 AtomicLong 的值
//        System.out.println(System.currentTimeMillis() - start);FutureTask<Integer> futureTask=new FutureTask<>(new MyThread2(100));Thread thread=new Thread(futureTask);thread.start();Integer o = futureTask.get();System.out.println(o);}
}class MyThread implements Runnable {private int count;public MyThread(int count) {this.count = count;}@Overridepublic void run() {int sum = 0;for (int i = 0; i <= count; i++) {sum += i;}// 使用 AtomicLong 的 addAndGet 方法进行原子更新ThreadLearn.result.addAndGet(sum);ThreadLearn.countDownLatch.countDown();}
}class MyThread1 extends Thread {private long count;MyThread1(int count) {this.count = count;}@Overridepublic void run() {int sum = 0;for (int i = 0; i <= count; i++) {sum += i;}// 使用 AtomicLong 的 addAndGet 方法进行原子更新ThreadLearn.result.addAndGet(sum);ThreadLearn.countDownLatch.countDown();}
}class MyThread2 implements Callable<Integer> {private int count;public MyThread2(int count) {this.count=count;}@Overridepublic Integer call() throws Exception {int sum = 0;for (int i = 0; i <= count; i++) {sum += i;}return sum;}
}

使用线程池

package Reflection;import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;public class ThreadLearn {// 使用 AtomicLong 替代 Long,保证线程安全且避免空指针问题public static AtomicLong result = new AtomicLong(0);static int count = 100000;public static CountDownLatch countDownLatch = new CountDownLatch(count);//线程池public static ExecutorService executor=Executors.newFixedThreadPool(10);public static void main(String[] args) throws InterruptedException, ExecutionException {Future<Integer> submit = executor.submit(new MyThread2(100));Integer i=submit.get();System.out.println(i);}
}class MyThread2 implements Callable<Integer> {private int count;public MyThread2(int count) {this.count=count;}@Overridepublic Integer call() throws Exception {int sum = 0;for (int i = 0; i <= count; i++) {sum += i;}return sum;}
}

2.统计多线程程序的运行时间

如果要统计多线程程序的执行时间,主线程等待所有子线程完成,那么总时间可能接近实际所有线程执行完毕的时间。所以可能需要确保主线程在所有子线程结束后才结束,然后计算整个程序的运行时间。

使用join方法

使用thread.join,` 让一个线程等待另一个线程执行完毕。在多线程编程中,有时我们需要确保某些线程在其他线程完成特定任务后再继续执行,这时就可以使用 join() 方法来实现线程间的同步。

package Reflection;import java.util.concurrent.atomic.AtomicLong;public class ThreadLearn {// 使用 AtomicLong 替代 Long,保证线程安全且避免空指针问题public static AtomicLong result = new AtomicLong(0);public static void main(String[] args) throws InterruptedException {long start = System.currentTimeMillis();int count = 10000; // 将线程数量和数组长度统一为 10000Thread[] threads = new Thread[count];for (int i = 0; i < count; i++) {MyThread myThread = new MyThread(100);threads[i] = new Thread(myThread);threads[i].start();}for (Thread thread : threads) {thread.join();}System.out.println(result.get()); // 使用 get 方法获取 AtomicLong 的值System.out.println(System.currentTimeMillis() - start);}
}class MyThread implements Runnable {private int count;public MyThread(int count) {this.count = count;}@Overridepublic void run() {int sum = 0;for (int i = 0; i <= count; i++) {sum += i;}// 使用 AtomicLong 的 addAndGet 方法进行原子更新ThreadLearn.result.addAndGet(sum);}
}

在这里插入图片描述

使用CountDownLatch

CountDownLatch 是 Java 并发包 java.util.concurrent 中的一个同步辅助类,主要用于协调多个线程之间的执行顺序。它可以让一个或多个线程等待其他一组线程完成它们的操作后再继续执行,从而确保程序的执行逻辑按照预期进行,避免出现数据不一致或逻辑错误的问题。

package Reflection;import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;public class ThreadLearn {// 使用 AtomicLong 替代 Long,保证线程安全且避免空指针问题public static AtomicLong result = new AtomicLong(0);static int count = 100000;public static CountDownLatch countDownLatch = new CountDownLatch(count);public static void main(String[] args) throws InterruptedException {long start = System.currentTimeMillis();Thread[] threads = new Thread[count];for (int i = 0; i < count; i++) {MyThread myThread = new MyThread(100);threads[i] = new Thread(myThread);threads[i].start();}countDownLatch.await();System.out.println(result.get()); // 使用 get 方法获取 AtomicLong 的值System.out.println(System.currentTimeMillis() - start);}
}class MyThread implements Runnable {private int count;public MyThread(int count) {this.count = count;}@Overridepublic void run() {int sum = 0;for (int i = 0; i <= count; i++) {sum += i;}// 使用 AtomicLong 的 addAndGet 方法进行原子更新ThreadLearn.result.addAndGet(sum);ThreadLearn.countDownLatch.countDown();}
}

执行结果:
在这里插入图片描述

CountDownLatch 的执行过程
1. 初始化阶段
  • 首先,需要创建一个 CountDownLatch 实例,在创建时要传入一个初始计数值。这个计数值代表了需要等待完成的操作数量。例如在代码中 CountDownLatch countDownLatch = new CountDownLatch(count);,这里的 count 就是初始计数值,它表明有 count 个任务需要完成,CountDownLatch 会将这个值存储在内部作为计数器的初始状态。
2. 任务线程启动阶段
  • 通常会有一个或多个任务线程被创建并启动去执行特定的任务。这些线程可能会并发地执行各自的任务,就像代码里通过循环创建并启动多个 Thread 实例,每个线程都去执行 MyThread 类的 run 方法中的任务。
3. 等待阶段
  • 有一个或多个线程(通常是主线程)会调用 CountDownLatchawait() 方法。当调用这个方法时,调用线程会进入阻塞状态,它会一直等待,直到 CountDownLatch 的内部计数器值变为 0。这意味着它在等待所有任务都完成。
4. 任务完成与计数器递减阶段
  • 每个任务线程在完成自己的任务后,会调用 CountDownLatchcountDown() 方法。这个方法的作用是将 CountDownLatch 的内部计数器值减 1。随着越来越多的任务线程完成任务并调用 countDown() 方法,计数器的值会不断减小。
5. 唤醒等待线程阶段
  • CountDownLatch 的内部计数器值减到 0 时,所有之前调用 await() 方法并处于阻塞状态的线程会被唤醒。这些线程会从 await() 方法处继续执行后续的代码逻辑。
6. 后续处理阶段
  • 被唤醒的线程可以继续执行后续的操作,比如代码中主线程在被唤醒后会打印最终的计算结果以及程序执行所花费的时间。
主要方法
  • CountDownLatch(int count) 构造方法:用于创建一个 CountDownLatch 实例,并初始化其内部计数器的值为 count。这个 count 表示需要等待完成的操作数量,必须是一个正整数。
  • void await() 方法:调用该方法的线程会进入阻塞状态,直到 CountDownLatch 的内部计数器值变为 0。如果在等待过程中当前线程被中断,会抛出 InterruptedException 异常。
  • boolean await(long timeout, TimeUnit unit) 方法:调用该方法的线程会等待一段时间,最多等待 timeout 时间(由 unit 指定时间单位)。如果在这段时间内计数器值变为 0,则线程会被唤醒并返回 true;如果超过指定时间计数器值仍不为 0,则线程会被唤醒并返回 false。同样,如果在等待过程中线程被中断,会抛出 InterruptedException 异常。
  • void countDown() 方法:该方法会将 CountDownLatch 的内部计数器值减 1。当计数器值减到 0 时,所有正在等待的线程会被唤醒。如果计数器值已经为 0,调用该方法不会产生任何效果。
  • long getCount() 方法:该方法用于返回 CountDownLatch 当前的计数器值。可以通过这个方法来查看还有多少个任务未完成。

3.如何解决多线程对共享变量操作的线程安全问题

package Reflection;public class ThreadLearn {public static long result ;public static void main(String[] args) throws InterruptedException {for (int i = 0; i < 10000; i++) {MyThread myThread = new MyThread(1000);Thread thread = new Thread(myThread);thread.start();}Thread.sleep(10*1000);System.out.println(result);}}
class MyThread implements Runnable {private int count;public MyThread(int count) {this.count = count;}@Overridepublic void run() {int sum = 0;for (int i = 0; i <= count; i++) {sum+=i;}ThreadLearn.result+=sum;System.out.println(sum);}
}

执行结果:
​​在这里插入图片描述

代码功能概述

这段 Java 代码主要的功能是创建 10000 个线程,每个线程会计算从 0 到 1000 的整数之和,然后将这个和累加到 ThreadLearn 类的静态变量 result 中。最后,主线程等待 10 秒后输出 result 的值。

代码存在的问题

  1. 线程安全问题ThreadLearn.result 是一个共享变量,多个线程同时对其进行写操作(ThreadLearn.result += sum;),这会导致数据竞争(Data Race)问题,最终的 result 值可能是错误的。
  2. 等待时间不确定性:使用 Thread.sleep(10 * 1000) 来等待所有子线程完成,这种方式不够可靠,因为不同的机器性能不同,可能会导致有些线程还未执行完,主线程就已经输出结果。

你提供的代码在多线程环境下存在线程安全问题,主要是因为多个线程同时对静态变量 ThreadLearn.result 进行写操作,这可能会导致数据竞争和不一致的结果。下面为你介绍几种解决该问题的方法。

方法一:使用 synchronized 关键字

synchronized 关键字可以用来修饰方法或代码块,保证同一时刻只有一个线程能够访问被修饰的代码,从而避免多线程对共享资源的并发访问问题。

package Reflection;public class ThreadLearn {public static long result;public static void main(String[] args) throws InterruptedException {for (int i = 0; i < 10000; i++) {MyThread myThread = new MyThread(1000);Thread thread = new Thread(myThread);thread.start();}Thread.sleep(10 * 1000);System.out.println(result);}
}class MyThread implements Runnable {private int count;public MyThread(int count) {this.count = count;}@Overridepublic void run() {int sum = 0;for (int i = 0; i <= count; i++) {sum += i;}// 使用 synchronized 块保证线程安全synchronized (ThreadLearn.class) {ThreadLearn.result += sum;}System.out.println(sum);}
}

解释:在 run 方法中,使用 synchronized (ThreadLearn.class)ThreadLearn.result += sum; 这行代码进行同步,这样同一时刻只有一个线程能够执行该代码块,从而保证了对 result 变量的线程安全访问。

执行结果:
在这里插入图片描述

方法二:使用 AtomicLong 类

AtomicLong 是 Java 提供的一个原子类,它提供了一些原子操作方法,可以保证对长整型变量的原子性更新,避免了使用 synchronized 带来的性能开销。

package Reflection;import java.util.concurrent.atomic.AtomicLong;public class ThreadLearn {// 使用 AtomicLong 代替 longpublic static AtomicLong result = new AtomicLong(0);public static void main(String[] args) throws InterruptedException {for (int i = 0; i < 10000; i++) {MyThread myThread = new MyThread(1000);Thread thread = new Thread(myThread);thread.start();}Thread.sleep(10 * 1000);System.out.println(result.get());}
}class MyThread implements Runnable {private int count;public MyThread(int count) {this.count = count;}@Overridepublic void run() {int sum = 0;for (int i = 0; i <= count; i++) {sum += i;}// 使用 AtomicLong 的 addAndGet 方法进行原子更新result.addAndGet(sum);System.out.println(sum);}
}

解释:将 ThreadLearn 类中的 result 变量类型改为 AtomicLong,并使用 addAndGet 方法来更新 result 的值。addAndGet 方法是原子操作,能够保证在多线程环境下对 result 的更新是线程安全的。

执行结果:
在这里插入图片描述

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/895283.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

django中间件,中间件给下面传值

1、新建middleware.py文件 # myapp/middleware.py import time from django.http import HttpRequest import json from django.http import JsonResponse import urllib.parse class RequestTimeMiddleware:def __init__(self, get_response):self.get_response get_respons…

Vision Transformer:打破CNN垄断,全局注意力机制重塑计算机视觉范式

目录 引言 一、ViT模型的起源和历史 二、什么是ViT&#xff1f; 图像处理流程 图像切分 展平与线性映射 位置编码 Transformer编码器 分类头&#xff08;Classification Head&#xff09; 自注意力机制 注意力图 三、Coovally AI模型训练与应用平台 四、ViT与图像…

深入浅出:探索 DeepSeek 的强大功能与应用

深入浅出&#xff1a;探索 DeepSeek 的强大功能与应用 在人工智能技术飞速发展的今天&#xff0c;自然语言处理&#xff08;NLP&#xff09;作为其重要分支&#xff0c;正逐渐渗透到我们生活的方方面面。DeepSeek 作为一款功能强大的 NLP 工具&#xff0c;凭借其易用性和高效性…

轮子项目--消息队列的实现(3)

上一篇文章中我把一些关键的类以及表示出来&#xff0c;如何对这些类对应的对象进行管理呢&#xff1f;管理分为硬盘和内存上&#xff0c;硬盘又分为数据库&#xff08;管理交换机&#xff0c;队列和绑定&#xff09;和文件&#xff08;管理消息&#xff09;&#xff0c;本文就…

【转载】开源鸿蒙OpenHarmony社区运营报告(2025年1月)

●截至2025年1月31日&#xff0c;开放原子开源鸿蒙&#xff08;OpenAtom OpenHarmony&#xff0c;简称“开源鸿蒙”或“OpenHarmony”&#xff09;社区累计超过8200名贡献者&#xff0c;共63家成员单位&#xff0c;产生51.2万多个PR、2.9万多个Star、10.5万多个Fork、68个SIG。…

双周报Vol.65:新增is表达式、字符串构造和数组模式匹配增强、IDE模式匹配补全增强...多项技术更新!

MoonBit更新 新增 is 表达式 这个表达式的语法形式为 expr is pat&#xff0c;这个表达式为 Bool 类型&#xff0c;当 expr 符合 pat 这个模式的时候返回 true&#xff0c;比如&#xff1a; fn use_is_expr(x: Int?) -> Unit {if x is Some(i) && i > 10 { .…

百问网imx6ullpro调试记录(linux+qt)

调试记录 文章目录 调试记录进展1.开发板相关1.1百问网乌班图密码 1.2 换设备开发环境搭建串口调试网络互通nfs文件系统挂载 1.3网络问题1.4系统启动1.5进程操作 2.QT2.1tslib1.获取源码2.安装依赖文件3.编译 2.2qt移植1.获取qt源码2.配置编译器3.编译 2.3拷贝到开发板1.拷贝2.…

C++模拟实现AVL树

目录 1.文章概括 2.AVL树概念 3.AVL树的性质 4.AVL树的插入 5.旋转控制 1.左单旋 2. 右单旋 3.左右双旋 4.右左双旋 6.全部代码 1.文章概括 本文适合理解平衡二叉树的读者阅读&#xff0c;因为AVL树是平衡二叉树的一种优化&#xff0c;其大部分实现逻辑与平衡二叉树是…

opc da 服务器数据 转 EtherCAT项目案例

目录 1 案例说明 2 VFBOX网关工作原理 3 应用条件 4 查看OPC DA服务器的相关参数 5 配置网关采集opc da数据 6 启动EtherCAT从站转发采集的数据 7 在服务器上运行仰科OPC DA采集软件 8 案例总结 1 案例说明 在OPC DA服务器上运行OPC DA client软件查看OPC DA服务器的相…

实验9 基于WebGoat平台的SQL注入攻击

实验9 基于WebGoat平台的SQL注入攻击 1.实验目的 熟悉WebGoat平台&#xff0c;在该平台上实现SQL注入攻击。 2.实验内容 &#xff08;1&#xff09;下载webgoat-server-8.2.2.jar。 &#xff08;2&#xff09;搭建java环境。 &#xff08;3&#xff09;运行webgoat。 &#xf…

StochSync:可在任意空间中生成360°全景图和3D网格纹理

StochSync方法可以用于在任意空间中生成图像&#xff0c;尤其是360全景图和3D网格纹理。该方法利用了预训练的图像扩散模型&#xff0c;以实现零-shot生成&#xff0c;消除了对新数据收集和单独训练生成模型的需求。StochSync 结合了 Diffusion Synchronization&#xff08;DS&…

HarmonyOS 5.0应用开发——全局自定义弹出框openCustomDialog

【高心星出品】 文章目录 全局自定义弹出框openCustomDialog案例开发步骤完整代码 全局自定义弹出框openCustomDialog CustomDialog是自定义弹出框&#xff0c;可用于广告、中奖、警告、软件更新等与用户交互响应操作。开发者可以通过CustomDialogController类显示自定义弹出框…

DeepSeek模型R1服务器繁忙,怎么解决?

在当今科技飞速发展的时代&#xff0c;人工智能领域不断涌现出令人瞩目的创新成果&#xff0c;其中DeepSeek模型无疑成为了众多关注焦点。它凭借着先进的技术和卓越的性能&#xff0c;在行业内掀起了一股热潮&#xff0c;吸引了无数目光。然而&#xff0c;如同许多前沿技术在发…

AIGC-微头条爆款文案创作智能体完整指令(DeepSeek,豆包,千问,Kimi,GPT)

Unity3D特效百例案例项目实战源码Android-Unity实战问题汇总游戏脚本-辅助自动化Android控件全解手册再战Android系列Scratch编程案例软考全系列Unity3D学习专栏蓝桥系列AIGC(GPT、DeepSeek、豆包、千问、Kimi)👉关于作者 专注于Android/Unity和各种游戏开发技巧,以及各种资…

[LLM面试题] 指示微调(Prompt-tuning)与 Prefix-tuning区别

一、提示调整(Prompt Tuning) Prompt Tuning是一种通过改变输入提示语&#xff08;input prompt&#xff09;以获得更优模型效果的技术。举个例子&#xff0c;如果我们想将一条英语句子翻译成德语&#xff0c;可以采用多种不同的方式向模型提问&#xff0c;如下图所示&#xf…

自主项目面试点总结

1、许苑–OJ判题系统 技术栈&#xff1a;Spring BootSpring Cloud AlibabaRedisMybatisMQDocker 项目地址: https://github.com/xuyuan-upward/xyoj-backend-microservice 1.1、项目介绍: 一个基于微服务的OJ系统&#xff0c;具备能够根据管理员预设的题目用例对用户提交的代…

【py】python安装教程(Windows系统,python3.13.2版本为例)

1.下载地址 官网&#xff1a;https://www.python.org/ 官网下载地址&#xff1a;https://www.python.org/downloads/ 2.64版本或者32位选择 【Stable Releases】&#xff1a;稳定发布版本&#xff0c;指的是已经测试过的版本&#xff0c;相对稳定。 【Pre-releases】&#…

CEF132 编译指南 MacOS 篇 - depot_tools 安装与配置 (四)

1. 引言 在 CEF132&#xff08;Chromium Embedded Framework&#xff09;的编译过程中&#xff0c;depot_tools 扮演着举足轻重的角色。这套由 Chromium 项目精心打造的脚本和工具集&#xff0c;专门用于获取、管理和更新 Chromium 及其相关项目&#xff08;包括 CEF&#xff…

1312:【例3.4】昆虫繁殖

1312&#xff1a;【例3.4】昆虫繁殖 时间限制: 1000 ms 内存限制: 65536 KB 提交数:60386 通过数: 29787 【题目描述】 科学家在热带森林中发现了一种特殊的昆虫&#xff0c;这种昆虫的繁殖能力很强。每对成虫过xx个月产yy对卵&#xff0c;每对卵要过两个月长成成虫…

单片机上SPI和IIC的区别

SPI&#xff08;Serial Peripheral Interface&#xff09;和IC&#xff08;Inter-Integrated Circuit&#xff09;是两种常用的嵌入式外设通信协议&#xff0c;它们各有优缺点&#xff0c;适用于不同的场景。以下是它们的详细对比&#xff1a; — 1. 基本概念 SPI&#xff0…