并发编程基础02-同步和异步

版权声明:博观而约取,厚积而薄发。 https://blog.csdn.net/BruceLiu_code/article/details/87946770

1.对象锁的同步和异步

同步:synchronized
同步的概念就是共享,我们只需要牢牢把握“共享”这个两个字就可以,如果不是共享的资源,就没有必要进行同步的操作。
异步:asynchronized
异步的概念就是独立,相互之间不受到相互的影响和制约。就好像我们在学习http的时候,在页面上发起一个Ajax请求,我们还可以继续浏览或者操作页面的内容,二者之间没有任何关系。
同步的目的就是为了线程到的安全,其实对于线程安全来说,需要满足两个特性:

  • 原子性(同步)
  • 可见性

示例1

/**
 * 对象的同步和异步的概念
 * @author bruceliu
 * @create 2019-02-26 20:18
 */
public class MyObject {

    public synchronized void method1() {
        try {
            System.out.println(Thread.currentThread().getName());
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void method2() {
        System.out.println(Thread.currentThread().getName());
    }

    public static void main(String[] args) {

        final MyObject object = new MyObject();

        /**
         * 分析:
         * t1线程先持有object对象的Lock锁,t2线程可以以异步的方式调用对象中的非synchronized修饰的方法
         */
        Thread t1 = new Thread(new Runnable() {
            public void run() {
                object.method1();
            }
        }, "t1");

        Thread t2 = new Thread(new Runnable() {
            public void run() {
                object.method2();
            }
        }, "t2");

        t1.start();
        t2.start();
    }
}

总结
t1线程先持有object对象的Lock锁,t2线程可以以异步的方式调用对象中的非synchronized修饰的方法。

示例2

/**
 * 对象的同步和异步的概念
 *
 * @author bruceliu
 * @create 2019-02-26 20:18
 */
public class MyObject {

    public synchronized void method1() {
        try {
            System.out.println(Thread.currentThread().getName());
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void method2() {
        System.out.println(Thread.currentThread().getName());
    }

    public static void main(String[] args) {

        final MyObject object = new MyObject();

        /**
         * 分析:
         * t1线程先持有object对象的Lock锁,t2线程如果在这个时候调用对象中的同步(synchronized)方法则需等待,也就是同步
         */
        Thread t1 = new Thread(new Runnable() {
            public void run() {
                object.method1();
            }
        }, "t1");

        Thread t2 = new Thread(new Runnable() {
            public void run() {
                object.method2();
            }
        }, "t2");

        t1.start();
        t2.start();
    }
}

总结
t1线程先持有object对象的Lock锁,t2线程如果在这个时候调用对象中的同步(synchronized)方法则需等待,也就是同步。

2.脏读

对于对象的同步和异步的方法,我们在设计自己的程序的时候,一定要考虑问题的整体性,不然就会出现数据的不一致性的错误,这种的错误就是脏读(dirtyread)
示例1

/**
 * 业务整体需要使用完整的synchronized,保持业务的原子性。
 *
 * @author bruceliu
 * @create 2019-02-26 20:32
 */
public class DirtyRead {

    private String username = "bruce";
    private String password = "123";

    public synchronized void setValue(String username, String password) {
        this.username = username;
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.password = password;
        System.out.println("setValue最终结果:username = " + username + " , password = " + password);
    }

    public void getValue() {
        System.out.println("getValue方法得到:username = " + this.username + " , password = " + this.password);
    }

    public static void main(String[] args) throws Exception {

        final DirtyRead dr = new DirtyRead();
        Thread t1 = new Thread(new Runnable() {
            public void run() {
                dr.setValue("tom", "456");
            }
        });
        t1.start();
        Thread.sleep(1000);

        dr.getValue();
    }
}

总结
在我们给一个对象的方法加锁的时候,需要考虑业务的整体性,即为setValue/getValue方法同时加锁synchronized 同步的关键字,保证业务(service)的原子性,不然会出现业务的错误(也从侧面保证业务的一致性)。

猜你喜欢

转载自blog.csdn.net/BruceLiu_code/article/details/87946770