java 线程之对象的同步和异步(实例讲解)
一、多线程环境下的同步与异步
同步:A线程要请求某个资源,但是此资源正在被B线程使用中,因为同步机制存在,A线程请求不到,怎么办,A线程只能等待下去。
package com.jalja.org.thread.demo01;
public class Thread02 {
public synchronized void method1(){
System.out.println("method1:"+Thread.currentThread().getName());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void method2(){
System.out.println("method2:"+Thread.currentThread().getName());
}
public static void main(String[] args) {
final Thread02 th=new Thread02();
Thread thread1=new Thread(new Runnable() {
public void run() {
th.method1();
}
},"th1");
Thread thread2=new Thread(new Runnable() {
public void run() {
th.method2();
}
},"th2");
thread1.start();
thread2.start();
}
}
观察输出结果:method1:th1 在3秒后 method2:th2 输出,这是因为method2() 与 method1()都是同步方法,而线程thread1 与 thread2操作的是同一个对象th,所以thread2在执行method2()方法时,需要先获得到th对象的锁。
异步:A线程要请求某个资源,但是此资源正在被B线程使用中,因为没有同步机制存在,A线程仍然请求的到,A线程无需等待。
package com.jalja.org.thread.demo01;
public class Thread02 {
public synchronized void method1(){
System.out.println("method1:"+Thread.currentThread().getName());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void method2(){
System.out.println("method2:"+Thread.currentThread().getName());
}
public static void main(String[] args) {
final Thread02 th=new Thread02();
Thread thread1=new Thread(new Runnable() {
public void run() {
th.method1();
}
},"th1");
Thread thread2=new Thread(new Runnable() {
public void run() {
th.method2();
}
},"th2");
thread1.start();
thread2.start();
}
}
观察输出结果:method1:th1 与 method2:th2 同时输出,这是因为method2 没有加同步控制,所以线程thread2在执行method2()方法时不用去获得执行权限(对象锁)。
二、数据的脏读
我们在设计业务的时候一定要考虑业务的整体性,不然就会出现数据一致性问题。
package com.jalja.org.thread.demo01;
public class Thread03 {
private String name="zs";
private String passWorrd="123";
public synchronized void setValue(String name,String passWord){
this.name=name;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.passWorrd=passWord;
System.out.println("set:name="+this.name +" passWorrd="+this.passWorrd);
}
public void getValue(){
System.out.println("get:name="+this.name +" passWorrd="+this.passWorrd);
}
public static void main(String[] args) throws InterruptedException {
final Thread03 th=new Thread03();
Thread thread=new Thread(new Runnable() {
public void run() {
th.setValue("LS", "456");
}
});
thread.start();
Thread.sleep(100);
th.getValue();
}
}
结果:get:name=LS passWorrd=123 set:name=LS passWorrd=456 由结果可知get的数据显然有问题,这是因为thread线程在set的时候,main线程在执行get方法。想要避免这种情况,我们就要保证当有线程在操作同一个对象的数据时,就不然其他线程也同时操作该对象的数据。这个情况我们在get方法上加 synchronized 关键字即可。
以上这篇java 线程之对象的同步和异步(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Spring Junit测试找不到SpringJUnit4ClassRunner.class的解决
这篇文章主要介绍了Spring Junit测试找不到SpringJUnit4ClassRunner.class的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-04-04
springboot自定义starter启动器的具体使用实践
本文主要介绍了springboot自定义starter启动器的具体使用实践,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2021-09-09
SpringBoot整合Flink CDC实现实时追踪mysql数据变动
我们将整合Spring Boot和Apache Flink CDC(Change Data Capture)来实现实时数据追踪,下面是一个基本的实践流程代码,包括搭建Spring Boot项目、整合Flink CDC以及实现数据变动的实时追踪,需要的朋友可以参考下2024-07-07
spring中使用@Autowired注解无法注入的情况及解决
这篇文章主要介绍了spring中使用@Autowired注解无法注入的情况及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-09-09
Java SpringMVC的@RequestMapping注解使用及说明
这篇文章主要介绍了Java SpringMVC的@RequestMapping注解使用及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-01-01
Spring Boot深入学习数据访问之Spring Data JPA与Hibernate的应用
Spring Data JPA是Spring Data的子项目,在使用Spring Data JPA之前,先了解一下Hibernate,因为Spring Data JPA是由Hibernate默认实现的2022-10-10
SpringBoot+Maven 多模块项目的构建、运行、打包实战
这篇文章主要介绍了SpringBoot+Maven 多模块项目的构建、运行、打包实战,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-05-05


最新评论