Development Tip

기본 메서드는 잠시 동안 true를 반환 한 다음 false를 반환합니까?

yourdevel 2020. 11. 15. 11:49
반응형

기본 메서드는 잠시 동안 true를 반환 한 다음 false를 반환합니까? (가능한 JVM 버그)


다음 코드에 문제가 있습니다. 가장 닫힌 형태로 분리했습니다. 거의 출시 준비가 된 Java 8을 사용하고 있습니다 (2014 년 3 월 18 일), 구현 자체에 심각한 문제가 없을 것으로 예상하므로 / 내 자신의 코드 여야합니다.

참고 : 코드는 default인터페이스의 메소드 구현을 포함하여 모든 종류의 새로운 기능이있는 Java 8로 작성되었습니다 .

public abstract class Drawable implements DrawableInterface {    

}

interface DrawableInterface {
    default public boolean isShadowReceiver() {
        return false;
    }

    default public boolean isShadowCaster() {
        return false;
    }
}

public interface ShadowDrawable extends DrawableInterface {
    @Override
    default public boolean isShadowReceiver() {
        return true;
    }

    @Override
    default public boolean isShadowCaster() {
        return true;
    }
}

public class Box extends Drawable implements ShadowDrawable {

}

public class IsolatedBug {
    private final Box box;

    private final List<Drawable> drawables;

    public IsolatedBug() {
        this.box = new Box();
        this.drawables = new ArrayList<>();

        drawables.add(box);
        drawables.forEach(drawable -> System.out.println(drawable + " C=" + drawable.isShadowCaster() + "/R=" + drawable.isShadowReceiver()));
    }

    private void init() throws InterruptedException {
        while (true) {
            drawables.forEach(drawable -> System.out.println(drawable + " C=" + drawable.isShadowCaster() + "/R=" + drawable.isShadowReceiver()));
            Thread.sleep(100);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        new IsolatedBug().init();
    }
}

코드 자체는 의미가 없을 수 있지만 다른 관련없는 메서드의 부하를 제거했기 때문입니다.

그러나 출력을 관찰하면 특정 시점에서 개인적으로 30 초 후에 이상한 것을 보게됩니다.

isolatedbug.Box@5acf9800 C = true / R = true
isolatedbug.Box@5acf9800 C = true / R = true
isolatedbug.Box@5acf9800 C = true / R = true
isolatedbug.Box@5acf9800 C = true / R = true
isolatedbug입니다. Box @ 5acf9800 C = false / R = false
isolatedbug.Box@5acf9800 C = false / R = false
isolatedbug.Box@5acf9800 C = false / R = false
isolatedbug.Box@5acf9800 C = false / R = false
isolatedbug.Box @ 5acf9800 C = false / R = false
isolatedbug.Box@5acf9800 C = false / R = false

에서 true전환하는 시간 false은 메서드 호출 횟수에 따라 달라지는 것 같습니다. 그 사이에 잠이 더 오래 걸리면 전환하는 데 더 오래 걸립니다.

Windows 8 64 비트에 대한 전체 정보를 보려면 다음을 실행하고 있습니다 java -version.

Java 버전 "1.8.0"
Java (TM) SE 런타임 환경 (빌드 1.8.0-b129)
Java HotSpot (TM) 64 비트 서버 VM (빌드 25.0-b69, 혼합 모드)

아무도 무슨 일이 일어나고 있는지 설명해 줄 수 있습니까?
Java 8-모든 빌드를 사용하는 다른 사람들이 동일한 문제가 있는지 실행하고 확인할 수 있다면 감사하겠습니다.

이 코드를 사용한 후 추가 정보 :

  Properties p = System.getProperties();
  p.list(System.out);

산출:

-- listing properties --
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=C:\Program Files\Java\jdk1.8.0\jre\bin
java.vm.version=25.0-b69
java.vm.vendor=Oracle Corporation
java.vendor.url=http://java.oracle.com/
path.separator=;
java.vm.name=Java HotSpot(TM) 64-Bit Server VM
file.encoding.pkg=sun.io
user.script=
user.country=NL
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=
java.vm.specification.name=Java Virtual Machine Specification
user.dir=C:\Users\Frank\Dropbox\NetbeansProjec...
java.runtime.version=1.8.0-b129
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=C:\Program Files\Java\jdk1.8.0\jre\li...
os.arch=amd64
java.io.tmpdir=C:\Users\Frank\AppData\Local\Temp\
line.separator=

java.vm.specification.vendor=Oracle Corporation
user.variant=
os.name=Windows 8.1
sun.jnu.encoding=Cp1252
java.library.path=C:\Program Files\Java\jdk1.8.0\bin;C:...
java.specification.name=Java Platform API Specification
java.class.version=52.0
sun.management.compiler=HotSpot 64-Bit Tiered Compilers
os.version=6.3
user.home=C:\Users\Frank
user.timezone=
java.awt.printerjob=sun.awt.windows.WPrinterJob
file.encoding=UTF-8
java.specification.version=1.8
user.name=Beheerder
java.class.path=C:\Users\Frank\Dropbox\NetbeansProjec...
java.vm.specification.version=1.8
sun.arch.data.model=64
java.home=C:\Program Files\Java\jdk1.8.0\jre
sun.java.command=isolatedbug.IsolatedBug
java.specification.vendor=Oracle Corporation
user.language=nl
awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode
java.version=1.8.0
java.ext.dirs=C:\Program Files\Java\jdk1.8.0\jre\li...
sun.boot.class.path=C:\Program Files\Java\jdk1.8.0\jre\li...
java.vendor=Oracle Corporation
file.separator=\
java.vendor.url.bug=http://bugreport.sun.com/bugreport/
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
sun.desktop=windows
sun.cpu.isalist=amd64

-XintVM 옵션 도 확인 했는데 이것이 사용되면 true예상대로 반환 됩니다.

따라서 결론은 내 특정 사용 사례에서 코드의 해석 및 JIT 컴파일 / 인라인 된 변형이 동일하지 않으므로 해석 된 코드가 컴파일 된 후 해석 된 코드에서 컴파일 된 코드로 전환 될 가능성이 있다는 것입니다. 출력의 스위치.

Adding the -Xint option to the actual program in which the bug occured, has also fixed the issue there.

The official bug report has been accepted: JIRA Bug JDK-8036100


This is a known bug in Java8.

See this Jira: CHA ignores default methods during analysis leading to incorrect code generation

This blog entry is enlightening....

Update / Summary:


Previous Notes

I have reproduced this issue with:

Claims that this issue is resolved in b127 are confusing since I see it clearly in b129 (unless I am confused about the JVM version conventions...)

C:\Java8\jdk-1.8.0_01\bin>java -version
java version "1.8.0"
Java(TM) SE Runtime Environment (build 1.8.0-b129)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b69, mixed mode)

C:\Java8\jdk-1.8.0_01\bin>

Adding System.out.println(System.getProperties());

{
java.runtime.name=Java(TM) SE Runtime Environment, 
java.runtime.version=1.8.0-b129, 
java.vm.specification.name=Java Virtual Machine Specification, 
java.vm.name=Java HotSpot(TM) 64-Bit Server VM, 
java.vm.version=25.0-b69, 
java.vm.vendor=Oracle Corporation, 
java.vendor.url=http://java.oracle.com/, 
java.vm.specification.version=1.8, 
java.specification.name=Java Platform API Specification, 
java.specification.version=1.8, 
java.specification.vendor=Oracle Corporation, 
java.class.version=52.0, 
sun.boot.library.path=C:\Java8\jdk-1.8.0_01\jre\bin, 
sun.java.launcher=SUN_STANDARD, 
sun.os.patch.level=Service Pack 1, 
java.endorsed.dirs=C:\Java8\jdk-1.8.0_01\jre\lib\endorsed, 
os.arch=amd64, 
java.vm.specification.vendor=Oracle Corporation, 
os.name=Windows 7, 
sun.jnu.encoding=Cp1252, 
java.library.path=C:\Java8\jdk-1.8.0_01\bin;......, 
sun.management.compiler=HotSpot 64-Bit Tiered Compilers, 
os.version=6.1, 
file.encoding=UTF-8, 
sun.java.command=fromso.IsolatedBug, 
java.home=C:\Java8\jdk-1.8.0_01\jre, 
sun.arch.data.model=64, 
user.language=en, 
java.ext.dirs=C:\Java8\jdk-1.8.0_01\jre\lib\ext;C:\windows\Sun\Java\lib\ext,

sun.boot.class.path=C:\Java8\jdk-1.8.0_01\jre\lib\resources.jar;......,
java.vendor=Oracle Corporation, 
file.separator=\, 
java.vendor.url.bug=http://bugreport.sun.com/bugreport/, 
sun.io.unicode.encoding=UnicodeLittle, 
sun.cpu.endian=little, 
sun.desktop=windows, 
sun.cpu.isalist=amd64
}

참고URL : https://stackoverflow.com/questions/22096052/default-method-returns-true-for-a-while-and-then-returns-false-possible-jvm-b

반응형