계: Code Quality
코드 품질이 낮으면 예측할 수 없는 동작이 발생합니다. 사용자 입장에서는 사용 편의성이 떨어지는 것으로 나타나는 경우가 많습니다. 공격자에게는 예상치 못한 방법으로 시스템에 부담을 줄 수 있는 기회가 됩니다.
Code Correctness: Non-Synchronized Method Overrides Synchronized Method
Abstract
Synchronized 메서드는 비 syncrhonized 메서드로 오버라이드되어서는 안 됩니다.
Explanation
상위 클래스는
예제 1: 다음 코드에서
이 경우
synchronized
메서드를 선언했고 여러 스레드가 동일한 인스턴스에 접근할 때 올바른 동작을 보장합니다. 또한 모든 오버라이드 메서드는 synchronized
로 선언되어야 합니다. 그렇지 않으면 예기치 못한 동작이 발생할 수도 있습니다.예제 1: 다음 코드에서
Foo
클래스는 Bar
클래스를 오버라이드하지만 synchronizedMethod
메서드를 synchronized
로 선언하지 않습니다.
public class Bar {
public synchronized void synchronizedMethod() {
for (int i=0; i<10; i++) System.out.print(i);
System.out.println();
}
}
public class Foo extends Bar {
public void synchronizedMethod() {
for (int i=0; i<10; i++) System.out.print(i);
System.out.println();
}
}
이 경우
Foo
의 인스턴스가 Bar
유형으로 배정될 수 있습니다. 같은 인스턴스를 서로 다른 두 스레드에 지정하고 synchronizedMethod
를 반복 실행하면 동작을 예측할 수 없게 됩니다.References
desc.structural.java.code_correctness_non_synchronized_method_overrides