同步方法与同步块的区别

来源:互联网 发布:驱动软件 知乎 编辑:程序博客网 时间:2024/05/16 15:02

java使用synchronized同步,分为四种情况:

  实例方法同步

  实例方法中同步块

  静态方法同步

  静态方法中同步块


我们从两个方面来说他们的不同,一个是同步方法和同步块的区别,一个是静态和非静态的区别。

同步方法就是在方法前加关键字synchronized,然后被同步的方法一次只能有一个线程进入,其他线程等待。而同步方法则是在方法内部使用大括号使得一个代码块得到同步。同步块会有一个同步的”目标“,使得同步块更加灵活一些(同步块可以通过”目标“决定需要锁定的对象)。一般情况下,如果此”目标“为this,那么同步方法和同步块没有太大的区别。

另外,通过反编译可以看出,同步块比同步方法多了两个指令。因此同步方法是比同步块要快一些。



非静态和静态的区别主要在于(以同步方法为例):非静态的同步方法是锁定类的实例的,而静态的同步方法是锁定类的;

也就是说,对于非静态的同步方法,在同一时刻,一个类的一个实例中,只有一个线程能进入同步的方法。但是对于多个实例,每一个实例的一个线程都可以进入同一同步的方法。

Demo1:一个实例的多个线程,一次只能有一个线程进入非静态同步的方法。

package SynchronizedTest;/** * Created by carrot on 16/8/31. */public class SyncFunc {    public synchronized void func1() {        System.out.println(Thread.currentThread().getName() + " is running");        try {            Thread.sleep(3000);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(Thread.currentThread().getName() + " is stop");    }    public static void main(String[] args) {        NewThread newThread1 = new NewThread();        NewThread newThread2 = new NewThread();        NewThread newThread3 = new NewThread();        newThread1.start();        newThread2.start();        newThread3.start();    }}class NewThread extends Thread {    static SyncFunc syncFunc = new SyncFunc();    @Override    public void run() {        syncFunc.func1();    }}

结果:

Thread-0 is runningThread-0 is stopThread-2 is runningThread-2 is stopThread-1 is runningThread-1 is stop

从结果可见,每次只能有一个线程进入非静态同步的方法。


Demo2:多个实例的线程能同时进入非静态同步的方法。

package SynchronizedTest;/** * Created by carrot on 16/8/31. */public class SyncFunc {    public synchronized void func1() {        System.out.println(Thread.currentThread().getName() + " is running");        try {            Thread.sleep(3000);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(Thread.currentThread().getName() + " is stop");    }    public static void main(String[] args) {        NewThread newThread1 = new NewThread();        NewThread newThread2 = new NewThread();        NewThread newThread3 = new NewThread();        newThread1.start();        newThread2.start();        newThread3.start();    }}class NewThread extends Thread {    SyncFunc syncFunc = new SyncFunc();    @Override    public void run() {        syncFunc.func1();    }}

结果:

Thread-0 is runningThread-1 is runningThread-2 is runningThread-0 is stopThread-2 is stopThread-1 is stop

从结果可以看出,多个实例的线程同时进入了同步的非静态方法。


Demo3:多个实例的线程进入静态的同步方法。

package SynchronizedTest;/** * Created by carrot on 16/8/31. */public class SyncFunc {    public static synchronized void func1() {        System.out.println(Thread.currentThread().getName() + " is running");        try {            Thread.sleep(3000);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(Thread.currentThread().getName() + " is stop");    }    public static void main(String[] args) {        NewThread newThread1 = new NewThread();        NewThread newThread2 = new NewThread();        NewThread newThread3 = new NewThread();        newThread1.start();        newThread2.start();        newThread3.start();    }}class NewThread extends Thread {    SyncFunc syncFunc = new SyncFunc();    @Override    public void run() {        syncFunc.func1();    }}

结果:

Thread-0 is runningThread-0 is stopThread-2 is runningThread-2 is stopThread-1 is runningThread-1 is stop

从结果可以看出,对于同一个对象的多个实例,在进入静态的同步方法时,一次只能有一个类实例进入。


1 0
原创粉丝点击