【发布时间】:2017-03-07 02:02:32
【问题描述】:
我是多线程的新手,我很困惑这个方法是否是线程安全的,因为我没有在 HttpURLConnection conn 上做一个新的...... .....................
protected byte[] someMethod(Authenticator authenticator, String url, boolean doPost) throws Exception {
try {
URL aUrl = new URL(url);
strBldr = new StringBuilder();
AuthenticatedURL.Token token = new AuthenticatedURL.Token();
TestConnectionConfigurator connConf = new TestConnectionConfigurator();
AuthenticatedURL authUrl = new AuthenticatedURL(authenticator, connConf);
HttpURLConnection conn = authUrl.openConnection(aUrl, token);
if (!connConf.invoked)
throw new IOException("failed to invoked");
String tokenStr = token.toString();
if (doPost) {
conn.setRequestMethod("POST");
conn.setDoOutput(true);
}
conn.setRequestProperty("Accept", "application/octet-stream");
conn.setDoOutput(true);
conn.connect();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedInputStream in = new BufferedInputStream(
conn.getInputStream());
ByteArrayOutputStream byteArraySt = new ByteArrayOutputStream();
int counter;
while ((counter = in.read()) != -1) {
byteArraySt.write(counter);
}
byte [] bArray = new byte[byteArraySt.toByteArray().length];
bArray = byteArraySt.toByteArray();
in.close();
return bArray;
}
【问题讨论】:
-
关于线程安全的更多信息:javaworld.com/article/2076747/core-java/…
-
如果所有变量都是本地的并且保持这种状态,线程安全问题就不可能出现。为什么将
doOutput设置为true,然后没有输出?
标签: java multithreading thread-safety