What is the reason why “synchronized” is not allowed in Java 8 interface methods?

后端 未结 2 2014
深忆病人
深忆病人 2020-12-02 03:31

In Java 8, I can easily write:

interface Interface1 {
    default void method1() {
        synchronized (this) {
            // Something
        }
    }

          


        
2条回答
  •  抹茶落季
    2020-12-02 04:15

    public class ParentSync {
    
    public synchronized void parentStart() {
        System.out.println("I am " + this.getClass() + " . parentStarting. now:" + nowStr());
        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("I am " + this.getClass() + " . parentFinished. now" + nowStr());
    }
    
    private String nowStr() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }
    }
    
    
    public class SonSync1 extends ParentSync {
    public void sonStart() {
        System.out.println("I am " + this.getClass() + ". sonStarting,calling parent now ... ");
        super.parentStart();
        System.out.println("I am " + this.getClass() + ". sonFinished");
    }
    }
    
    
    
    public class SonSync2 extends ParentSync {
    
    public void sonStart() {
        System.out.println("I am " + this.getClass() + ". sonStarting,calling parent now ... ");
        super.parentStart();
        System.out.println("I am " + this.getClass() + ". sonFinished");
    }
    }
    
    
    
    public class SyncTest {
    public static void main(String[] args) throws Exception {
    
        new Thread(() -> {
            new SonSync1().sonStart();
        }).start();
    
        new Thread(() -> {
            new SonSync2().sonStart();
        }).start();
    
        System.in.read();
    }
    }
    

    result:

    I am class com.common.interface18_design.whynotsync_onmethod.SonSync1. sonStarting,calling parent now ... 
    I am class com.common.interface18_design.whynotsync_onmethod.SonSync2. sonStarting,calling parent now ... 
    I am class com.common.interface18_design.whynotsync_onmethod.SonSync2 . parentStarting. now:2019-04-18 09:50:08
    I am class com.common.interface18_design.whynotsync_onmethod.SonSync1 . parentStarting. now:2019-04-18 09:50:08
    I am class com.common.interface18_design.whynotsync_onmethod.SonSync1 . parentFinished. now2019-04-18 09:50:38
    I am class com.common.interface18_design.whynotsync_onmethod.SonSync1. sonFinished
    I am class com.common.interface18_design.whynotsync_onmethod.SonSync2 . parentFinished. now2019-04-18 09:50:38
    I am class com.common.interface18_design.whynotsync_onmethod.SonSync2. sonFinished
    

    (sorry for using parent class as example)

    from the result , we could know that the parent class lock is owned by every sub class, SonSync1 and SonSync2 object have different object lock . every lock is independency. so in this case , i think it is not dangerous using a synchronized in a parent class or a common interface . could anyone explain more on this ?

提交回复
热议问题