synchronized,同步,同步方法,同步语句

同步

* 关键字synchronized 创建同步方法

* 当第二个子线程开始执行时,他在第一个子线程使用完同步方法之前是不会进入同步方法的

* 对于任何给定的对象,一旦同步方法被调用,就会锁住对象,其他线程的执行就不能使用同一对象上的同步方法

* 其他线程试图使用正在使用的对象时将进入等待状态,直到对象解锁为止。

* 当对象离开同步方法时,对象被解锁

同步方法

public class SumArray {
    private int sum;

    synchronized int SumArray(int nums[]) {
        sum = 0;
        for (int i=0; i<nums.length; i++) {
            sum += nums[i];
            System.out.println("Running total for " + Thread.currentThread().getName() + " is " + sum);
            try {
                Thread.sleep(10);
            }
            catch (InterruptedException exception) {
                System.out.println("Thread interrupted");
            }
        }
        return sum;
    }
}

public class MyThread3 implements Runnable {
    Thread thread;
    static SumArray sumArray = new SumArray();
    int a[];
    int answer;

    MyThread3(String name, int nums[]) {
        thread = new Thread(this, name);
        a =nums;
    }
    public static MyThread3 createAndStart(String name, int nums[]) {
        MyThread3 myThread = new MyThread3(name, nums);
        myThread.thread.start();
        return myThread;
    }
    public void run() {
        //int sum;
        System.out.println(thread.getName() + " starting.");
        answer = sumArray.SumArray(a);
        System.out.println("Sum for " + thread.getName() + " is " + answer);
        System.out.println(thread.getName() + " terminating.");
    }
}

public class Sync {
    public static void main(String[] args) {
        int a[] = {1, 2, 3, 4, 5};
        MyThread3 mt1 = MyThread3.createAndStart("Child #1", a);
        MyThread3 mt2 = MyThread3.createAndStart("Child #2", a);
        try {
            mt1.thread.join();
            mt2.thread.join();
        }
        catch (InterruptedException exception) {
            System.out.println("Main thread interrupted.");
        }
    }
}

运行程序,可以试着对比去掉synchronized关键字和加上时,结果的不同

当去掉关键字,SumArray()方法不受同步保护,两个线程会同时操作,得到的answer不是[1,3,6,10,15]

同步语句

synchronized(objref) {  

        //statements to be synchronized

}

这里,objerf 是对被同步对象的引用。一旦进入同步代码块,在退出代码块之前,其他线程将不再能调用objerf引用的对象的同步方法。

​
public class SumArray {
    private int sum;

    int SumArray(int nums[]) {
        sum = 0;
        for (int i=0; i<nums.length; i++) {
            sum += nums[i];
            System.out.println("Running total for " + Thread.currentThread().getName() + " is " + sum);
            try {
                Thread.sleep(10);
            }
            catch (InterruptedException exception) {
                System.out.println("Thread interrupted");
            }
        }
        return sum;
    }
}

public class MyThread3 implements Runnable {
    Thread thread;
    static SumArray sumArray = new SumArray();
    int a[];
    int answer;

    MyThread3(String name, int nums[]) {
        thread = new Thread(this, name);
        a =nums;
    }
    public static MyThread3 createAndStart(String name, int nums[]) {
        MyThread3 myThread = new MyThread3(name, nums);
        myThread.thread.start();
        return myThread;
    }
    public void run() {
        //int sum;
        System.out.println(thread.getName() + " starting.");
        synchronized(sa) {//这里,在sa对象上对sumArray()的调用被同步
            answer = sumArray.SumArray(a);
        }
        System.out.println("Sum for " + thread.getName() + " is " + answer);
        System.out.println(thread.getName() + " terminating.");
    }
}

public class Sync {
    public static void main(String[] args) {
        int a[] = {1, 2, 3, 4, 5};
        MyThread3 mt1 = MyThread3.createAndStart("Child #1", a);
        MyThread3 mt2 = MyThread3.createAndStart("Child #2", a);
        try {
            mt1.thread.join();
            mt2.thread.join();
        }
        catch (InterruptedException exception) {
            System.out.println("Main thread interrupted.");
        }
    }
}

​

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值