【发布时间】:2011-05-25 10:29:51
【问题描述】:
今天我遇到了org.jasig.cas.client.util.CommonUtils类的方法constructServiceUrl()。我觉得他很奇怪:
final StringBuffer buffer = new StringBuffer();
synchronized (buffer)
{
if (!serverName.startsWith("https://") && !serverName.startsWith("http://"))
{
buffer.append(request.isSecure() ? "https://" : "http://");
}
buffer.append(serverName);
buffer.append(request.getRequestURI());
if (CommonUtils.isNotBlank(request.getQueryString()))
{
final int location = request.getQueryString().indexOf(
artifactParameterName + "=");
if (location == 0)
{
final String returnValue = encode ? response.encodeURL(buffer.toString()) : buffer.toString();
if (LOG.isDebugEnabled())
{
LOG.debug("serviceUrl generated: " + returnValue);
}
return returnValue;
}
buffer.append("?");
if (location == -1)
{
buffer.append(request.getQueryString());
}
else if (location > 0)
{
final int actualLocation = request.getQueryString()
.indexOf("&" + artifactParameterName + "=");
if (actualLocation == -1)
{
buffer.append(request.getQueryString());
}
else if (actualLocation > 0)
{
buffer.append(request.getQueryString().substring(0, actualLocation));
}
}
}
}
作者为什么要同步一个局部变量?
【问题讨论】:
-
无论正在同步什么,同步是否会产生其他副作用?
-
@TREE,没有。只要没有其他线程可以看到正在同步的对象,它就不需要任何效果。
-
对这个问题的接受回答澄清了我错的原因:stackoverflow.com/questions/1850270/…
标签: java multithreading