[英]Is a method thread safe in Java if it only access a thread safe object?
Suppose to use an object of a Java class declared to be thread safe (eg HttpClient
from Apache Commons ) as follows: 假设使用声明为线程安全的Java类的对象(例如来自Apache Commons的 HttpClient
),如下所示:
public class MyClass{
private HttpClient myClient;
public HttpClient getClient(){
return myClient;
}
}
Is the getClient()
method thread safe since myClient
is thread safe and I only return it or I must declare it synchronized
or I need to use a Lock
to declare it thread safe? getClient()
方法线程是否安全,因为myClient
是线程安全的 ,我只返回它,或者我必须声明它是synchronized
或者我需要使用Lock
来声明它是否安全?
您不需要声明同步的东西,因为getClient不会为不同的getClient调用返回不同的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.