How can weakCompareAndSet fail spuriously if it is implemented exactly like compareAndSet?

前端 未结 2 760
庸人自扰
庸人自扰 2020-12-24 05:00

(note that this question is not about CAS, it\'s about the \"May fail spuriously\" Javadoc).

The only difference in the Javadoc between these two methods fr

相关标签:
2条回答
  • 2020-12-24 05:43

    There is a difference between implementation and specification...

    Whilst on a particular implementation there may not be much point in providing different implementations, future implementations perhaps on different hardware may want to. Whether this method carries its weight in the API is debatable.

    Also the weak methods do not have happens-before ordering defined. The non-weak versions behave like volatile fields.

    0 讨论(0)
  • 2020-12-24 06:03

    Just to play a bit, if your question were

    How can weakDoIt fail spuriously if it is implemented exactly like doIt?

    here is the answer!

    public void doIt() {
        a();
    }
    
    /**
     * May fail spuriously
     */
    public void weakDoIt() {
        a();
    }
    
    void a(){
        if(Thread.currentThread().getStackTrace()[2].toString().contains("weakDoIt"))
            System.out.println("I will fail spuriously!");
        else System.out.println("I won't fail spuriously!");
    }
    
    0 讨论(0)
提交回复
热议问题