AtomicReference를 사용하여 최후의 단일 작업 함정 막기

14Jun10

public PlayerScore getHighScore() {
ServletContext ctx = getServletConfig().getServletContext();
AtomicReference holder = (AtomicReference) ctx.getAttribute("highScore");
return holder.get();
}

public void updateHighScore(PlayerScore newScore) {
ServletContext ctx = getServletConfig().getServletContext();
AtomicReference holder = (AtomicReference) ctx.getAttribute("highScore");
while (true) {
HighScore old = holder.get();
if (old.score >= newScore.score)
break;
else if (holder.compareAndSet(old, newScore))
break;
}
}

Advertisement


No Responses Yet to “AtomicReference를 사용하여 최후의 단일 작업 함정 막기”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.