当前位置: 首页 > wzjs >正文

中国优秀设计网站有哪些免费建立自己的网站

中国优秀设计网站有哪些,免费建立自己的网站,公众号网站开发,网站怎么在移动端推广在Java并发编程中,可见性、原子性和有序性是保证多线程程序正确性的三个重要特性: 1. 原子性(Atomicity) 定义:原子性指的是一个操作是不可中断的,要么全部执行成功,要么全部不执行。就好像是…

在Java并发编程中,可见性、原子性和有序性是保证多线程程序正确性的三个重要特性:

1. 原子性(Atomicity)

  • 定义:原子性指的是一个操作是不可中断的,要么全部执行成功,要么全部不执行。就好像是一个“原子”,不可再分。在Java中,对基本数据类型(除longdouble在某些平台上)的简单读写操作是原子的,但像i++这样的复合操作不是原子的。
  • 示例
public class AtomicityExample {private static int count = 0;public static void increment() {count++;}public static void main(String[] args) throws InterruptedException {Thread[] threads = new Thread[1000];for (int i = 0; i < 1000; i++) {threads[i] = new Thread(AtomicityExample::increment);}for (Thread thread : threads) {thread.start();}for (Thread thread : threads) {thread.join();}System.out.println("Final count: " + count);}
}

在上述代码中,count++ 操作不是原子的,它包含读取 count 的值、增加 1、再写回 count 三个步骤。在多线程环境下,可能会出现数据竞争,导致最终的 count 值小于预期的 1000。

  • 解决方法
    • 使用 synchronized 关键字:通过对代码块或方法加锁,保证同一时间只有一个线程能执行该代码块,从而保证原子性。
public class AtomicitySynchronizedExample {private static int count = 0;public static synchronized void increment() {count++;}public static void main(String[] args) throws InterruptedException {Thread[] threads = new Thread[1000];for (int i = 0; i < 1000; i++) {threads[i] = new Thread(AtomicitySynchronizedExample::increment);}for (Thread thread : threads) {thread.start();}for (Thread thread : threads) {thread.join();}System.out.println("Final count: " + count);}
}
- **使用原子类**:如 `AtomicInteger`、`AtomicLong` 等,它们提供了原子操作方法。
import java.util.concurrent.atomic.AtomicInteger;public class AtomicityAtomicExample {private static AtomicInteger count = new AtomicInteger(0);public static void increment() {count.incrementAndGet();}public static void main(String[] args) throws InterruptedException {Thread[] threads = new Thread[1000];for (int i = 0; i < 1000; i++) {threads[i] = new Thread(AtomicityAtomicExample::increment);}for (Thread thread : threads) {thread.start();}for (Thread thread : threads) {thread.join();}System.out.println("Final count: " + count.get());}
}

2. 可见性(Visibility)

  • 定义:可见性是指当一个线程修改了共享变量的值,其他线程能够立即得知这个修改。在多线程环境中,由于每个线程都有自己的工作内存,线程对共享变量的操作是在工作内存中进行的,而不是直接操作主内存中的变量,这就可能导致一个线程对共享变量的修改,其他线程不能及时看到。
  • 示例
public class VisibilityExample {private static boolean flag = false;public static void main(String[] args) {new Thread(() -> {while (!flag) {// 线程1在等待flag变为true}System.out.println("线程1结束等待");}).start();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}new Thread(() -> {flag = true;System.out.println("线程2修改了flag");}).start();}
}

在上述代码中,线程2修改了 flag 变量,但线程1可能一直无法感知到这个变化,导致线程1无限循环。

  • 解决方法
    • 使用 volatile 关键字:被 volatile 修饰的变量,线程对其修改会立即同步到主内存,并且其他线程读取该变量时会强制从主内存获取最新值。
public class VisibilityVolatileExample {private static volatile boolean flag = false;public static void main(String[] args) {new Thread(() -> {while (!flag) {// 线程1在等待flag变为true}System.out.println("线程1结束等待");}).start();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}new Thread(() -> {flag = true;System.out.println("线程2修改了flag");}).start();}
}
- **使用 `synchronized` 关键字**:进入 `synchronized` 代码块时,线程会从主内存刷新共享变量的值到工作内存,退出时会将工作内存变量值写回主内存,也能保证可见性。

3. 有序性(Ordering)

  • 定义:有序性是指程序执行的顺序按照代码的先后顺序执行。但在实际执行中,为了提高性能,编译器和处理器可能会对指令进行重排序,在单线程环境下,指令重排序不会影响最终结果,但在多线程环境下可能导致问题。
  • 示例
public class OrderingExample {private static int a = 0;private static int b = 0;public static void main(String[] args) throws InterruptedException {Thread thread1 = new Thread(() -> {a = 1; // 语句1b = 2; // 语句2});Thread thread2 = new Thread(() -> {if (b == 2) { // 语句3System.out.println(a); // 语句4}});thread1.start();thread2.start();thread1.join();thread2.join();}
}

在上述代码中,理想情况下线程1先执行完 a = 1b = 2,线程2执行时 b == 2 为真,会输出 1。但由于指令重排序,线程1可能先执行 b = 2,然后执行 a = 1,此时线程2执行时 b == 2 为真,但 a 可能还未被赋值为 1,输出结果可能为 0

  • 解决方法
    • 使用 volatile 关键字volatile 关键字具有禁止指令重排序的语义,保证 volatile 变量的读写操作顺序与代码顺序一致。
    • 使用 synchronized 关键字synchronized 块中的代码也具有顺序性,同一时刻只有一个线程能进入 synchronized 块,从而避免指令重排序带来的问题。

可见性、原子性和有序性是Java并发编程中非常重要的概念,理解并正确应用它们可以帮助我们编写正确、高效的多线程程序。

http://www.dtcms.com/wzjs/136166.html

相关文章:

  • 模板网站怎么建设优化seo品牌优化整站优化
  • 做网站月薪发布友情链接
  • 怎么做区块链网站网络推广渠道有哪些
  • 商户网站建设公司网站怎么申请怎么注册
  • 58同城类似的网站怎么做seo学习
  • 如何做代刷网站海南百度推广公司电话
  • 企业邮箱认证石家庄seo网络推广
  • dede调用其他网站数据库官方推广平台
  • 建设有一个网站需要什么seo营销推广多少钱
  • 做赚钱网站市场调研流程
  • 怎样创建个人购物网站windows优化软件
  • 政府网站建设请示百度搜索广告
  • 如何使用qq邮箱做网站谷歌搜索引擎优化seo
  • 协会网站模板网站seo优化徐州百度网络
  • 网站设计怎么做ppt答辩google关键词查询工具
  • 秦皇岛建设局官方网站优化网站搜索排名
  • 浅谈网站页面框架设计深圳网络推广哪家
  • wordpress调整meta重庆网站优化软件
  • 大会注册网站建设精准引流获客软件
  • 海口疫情最新消息今天封城了百度seo排名优化技巧分享
  • 网站建设0doit精品成品网站入口
  • 可以查授权的网站怎么做甲马营seo网站优化的
  • 赣州建设监督网站全国疫情突然又严重了
  • 海外网站加速器下载汕头百度网络推广
  • 北京移动网站建设seo搜索引擎优化包邮
  • 局政府网站建设总结关系营销案例
  • 日本站外网站怎么做图片优化
  • 自己做外贸网站能接到单吗网店怎么运营和推广
  • 做网站标题seo渠道
  • 上海建设局网站 招聘seo系统培训课程