欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java 线程之对象的同步和异步(实例讲解)

程序员文章站 2022-10-13 09:47:54
一、多线程环境下的同步与异步 同步:a线程要请求某个资源,但是此资源正在被b线程使用中,因为同步机制存在,a线程请求不到,怎么办,a线程只能等待下去。 pack...

一、多线程环境下的同步与异步

同步: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 线程之对象的同步和异步(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。