Skip to content

[아이템 84] 프로그램의 동작을 스레드 스케줄러에 기대지 말라 #87

Description

@zziri

정확성이나 성능이 스레드 스케줄러에 따라 달라지는 프로그램이라면 다른 플랫폼에 이식하기 어려움

  • 내 M1에서는 되는데...

멀티 스레드 환경에서 주의할 점

실행 가능한 스레드의 평균적인 수를 프로세서(core) 수보다 지나치게 많아지지 않도록 하는 것

  • 실행 가능한 스레드의 수와 전체 스레드 수는 구분해야함. 대기 중인 스레드는 실행 가능하지 않음.

스레드는 당장 처리해야할 작업이 없다면 실행되어서는 안됨

  • 스레드는 절대 busy waiting 상태가 되면 안됨 (절대..?)

극단적으로 삐딱한 구현

public class SlowCountDownLatch {
  private int count;
  public SlowCountDownLatch(int count) {
    if (count < 0)
      throw new new IllegalArgumentException(count + " < 0");
    this.count = count;
  }
  public void await() {
    while (true) {
      synchronized(this) {
        if (count == 0)
          return;
     }
    }
  }
}

https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/java/util/concurrent/CountDownLatch.java

Thread.yield

Thread.yield를 써서 문제를 고쳐보려는 유혹을 떨쳐내자

  • 현재 실행중인 동등한 우선순위 이상의 다른 스레드에 양보하는 것

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions