码迷,mamicode.com
首页 > 编程语言 > 详细

Java 锁

时间:2015-06-17 16:38:09      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:并发

Java 锁

定义

正确性

某个类的行为与其规范完全一致

原子性

读取-修改-写入作为不可分割的一个动作执行

竞态条件

当某个计算的正确性取决于多线程的交替执行时序,那么就会发生竞态条件

synchronized

定义:Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码。

当两个并发线程访问同一个对象object中的这个synchronized(this)同步代码块时,一个时间内只能有一个线程得到执行。另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。

当两个并发线程访问同一个类的不同对象中的synchronized(this)同步代码块时,一个时间段内两个对象可以交替执行。

当两个并发线程访问同一个类的不同对象中的synchronized(Class)同步代码块时,一个时间内只能有一个线程得到执行。另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。

当两个并发线程访问不同类的不同对象中的synchronized(Class)同步代码块时,一个时间段内两个对象可以交替执行,执行顺序不可预测。

因此可以得出总结synchronized可以修饰任何对象,及时添加了synchronized,程序还是可能会产生静态条件。

package com.company.chap2;


public class SynThread1 implements Runnable{
    private Data data;

    public SynThread1(Data data) {
        this.data = data;
    }

    @Override
    public void run() {
        synchronized (SynThread1.this) {
            try {
                data.setName("CommonThread1");
                System.out.println("logical 1");
                Thread.sleep((int)(Math.random() * 1000));//CPU计算
                System.out.println("logical 1-1");
                data.setAge(1);
                System.out.println("logical 2");
                Thread.sleep((int)(Math.random() * 1000));//CPU计算
                System.out.println("logical 2-2");
                data.setSex(true);
                System.out.println("logical 3");
                Thread.sleep((int)(Math.random() * 1000));//CPU计算
                System.out.println("logical 3-3");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                System.out.println(data.toString());
            }
        }
    }
}
package com.company.chap2;


public class SynThread2 implements Runnable{
    private Data data;

    public SynThread2(Data data) {
        this.data = data;
    }

    @Override
    public void run() {
        try {
            data.setName("CommonThread2");
            System.out.println("logical 4");
            Thread.sleep((int)(Math.random() * 1000));//CPU计算
            System.out.println("logical 4-4");
            data.setAge(1);
            System.out.println("logical 5");
            Thread.sleep((int)(Math.random() * 1000));//CPU计算
            System.out.println("logical 5-5");
            data.setSex(true);
            System.out.println("logical 6");
            Thread.sleep((int)(Math.random() * 1000));//CPU计算
            System.out.println("logical 6-6");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println(data.toString());
        }
    }
}
package com.company.chap2;


public class CommonThread1 implements Runnable{
    private Data data;

    public CommonThread1(Data data) {
        this.data = data;
    }

    @Override
    public void run() {
        try {
            data.setName("CommonThread1");
            System.out.println("logical 1");
            Thread.sleep((int)(Math.random() * 1000));//CPU计算
            System.out.println("logical 1-1");
            data.setAge(1);
            System.out.println("logical 2");
            Thread.sleep((int)(Math.random() * 1000));//CPU计算
            System.out.println("logical 2-2");
            data.setSex(true);
            System.out.println("logical 3");
            Thread.sleep((int)(Math.random() * 1000));//CPU计算
            System.out.println("logical 3-3");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println(data.toString());
        }
    }
}
package com.company.chap2;


public class CommonThread2 implements Runnable{
    private Data data;

    public CommonThread2(Data data) {
        this.data = data;
    }

    @Override
    public void run() {
        try {
            data.setName("CommonThread2");
            System.out.println("logical 4");
            Thread.sleep((int)(Math.random() * 1000));//CPU计算
            System.out.println("logical 4-4");
            data.setAge(2);
            System.out.println("logical 5");
            Thread.sleep((int)(Math.random() * 1000));//CPU计算
            System.out.println("logical 5-5");
            data.setSex(false);
            System.out.println("logical 6");
            Thread.sleep((int)(Math.random() * 1000));//CPU计算
            System.out.println("logical 6-6");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println(data.toString());//输出结果
        }
    }
}
    //测试程序
    package com.company.chap2;

public class Test {
    public static void main(String args[]) {
        Data data = new Data("niuwei", 18, true);
        //CommonThread1 commonThread1 = new CommonThread1(data);
        //CommonThread2 commonThread2 = new CommonThread2(data);
        SynThread1 synThread1 = new SynThread1(data);
        SynThread1 synThread2 = new SynThread1(data);

        print("Start");
        //new Thread(commonThread1).start();
        //new Thread(commonThread2).start();
        new Thread(synThread1).start();
        new Thread(synThread1).start();
        print("End");
    }

    public static void print(Object o) {
        System.out.println(o.toString());
    }
}

解释

首先

CommonThread1 commonThread1 = new CommonThread1(data);
CommonThread2 commonThread2 = new CommonThread2(data);
new Thread(commonThread1).start();
new Thread(commonThread2).start();
//输出结果
Start
logical 1
logical 4
End
logical 1-1
logical 2
logical 2-2
logical 3
logical 4-4
logical 5
logical 3-3
Name: CommonThread2, Age: 2, Sex = 男
logical 5-5
logical 6
logical 6-6
Name: CommonThread2, Age: 2, Sex = 女

因为当Thread.sleep()期间,线程发生阻塞,JVM调度处于就绪态的线程,因而交替执行。

然后

SynThread1 synThread1 = new SynThread1(data);
SynThread1 synThread2 = new SynThread1(data);
new Thread(synThread1).start();
new Thread(synThread1).start();
new Thread(synThread2).start();
Start
logical 1
End
logical 1
logical 1-1
logical 2
logical 1-1
logical 2
logical 2-2
logical 3
logical 2-2
logical 3
logical 3-3
Name: CommonThread1, Age: 1, Sex = 男
logical 3-3
Name: CommonThread1, Age: 1, Sex = 男
logical 1
logical 1-1
logical 2
logical 2-2
logical 3
logical 3-3
Name: CommonThread1, Age: 1, Sex = 男

Java 锁

标签:并发

原文地址:http://blog.csdn.net/u010107250/article/details/46535049

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!