简体   繁体   English

Java Apache HttClient:ThreadSafe吗?

[英]Java Apache HttClient : ThreadSafe or not?

I have to create a web service which interrogate a black box via an http request (method get). 我必须创建一个Web服务,该服务通过http请求(方法get)来询问黑匣子。

I would like to use Apache http client, but with 4.3.1 version i have some warning about deprecated object & methods. 我想使用Apache http客户端,但是在4.3.1版本中,我有一些关于不赞成使用的对象和方法的警告。

All example explain to use DefaultHttpClient which is ThreadSafe. 所有示例均说明使用DefaultHttpClient(即ThreadSafe)。 Now we have to use HttClientBuilder which is NOT ThreadSafe. 现在我们必须使用不是ThreadSafe的HttClientBuilder。

What is impact about that exactly ? 这到底有什么影响? Stupid question : if i use it in a Spring MVC Controller (Singleton threadsafe), what are the consequences ? 愚蠢的问题:如果我在Spring MVC控制器(Singleton线程安全)中使用它,会有什么后果? Is a not thread safe process used in a thread safe become thread safe (lol) ? 线程安全中使用的非线程安全过程是否变为线程安全(lol)?

下面的代码返回一个CloseableHttpClient ,如果您认为它是线程安全的。

HttpClientBuilder.create().build();

I don't know exactly, what you want to do, but concurrency is all about sharing state between threads. 我不确切知道您想做什么,但是并发性全在于线程之间共享状态。 If two threads access the same state in parallel, you might/will get problems. 如果两个线程并行访问同一状态,则可能/将遇到问题。

Note that local variables are NOT shared, so if multiple threads invoke the same method in parallel, all of them have their own local variables: 请注意,不共享局部变量,因此,如果多个线程并行调用同一方法,则它们都具有自己的局部变量:

public int safeAdd(int a, int b) {
    int sum = a + b;
    return sum;
}

The local variables a , b and sum are NOT shared state! 局部变量absum不处于共享状态! Every thread gets its own 'copy' of them! 每个线程都有自己的“副本”! So the object is stateless! 因此对象是无状态的!

Stateless objects are always thread-safe! 无状态对象始终是线程安全的!

Of course, things change if you publish you local variables: 当然,如果发布局部变量,情况会发生变化:

int sum;

public int unsafeAdd(int a, int b) {
    sum = a + b;
    return sum;
}

Here, sum is not a local variable any more. 在这里, sum不再是局部变量。 It exists only once (per instance). 它仅存在一次(每个实例)。 If multiple threads call unsafeAdd , something bad/unexpected might/will happen! 如果多个线程调用unsafeAdd ,则可能会/将会发生不良/意外事件!

This means, if you use your HttpClientBuilder only within your methods - no problem. 这意味着,如果仅在您的方法中使用HttpClientBuilder ,则没问题。 But if you publish it (as in the example above), you must take care of parallel access. 但是,如果您发布它(如上例所示),则必须注意并行访问。

(See also book "Java Concurrency in Practice": http://jcip.net/ ) (另请参见“实践中的Java并发性”一书: http//jcip.net/

Seems you need to understand what thread-safe means first. 似乎您需要先了解线程安全的含义。

Class X being thread safe generally means that if you have a class variable Y (of type X) and you use it from multiple threads, this will not lead to any problems (eg because X's public methods are guarded/synchronized). X类是线程安全的,通常意味着如果您有一个类变量Y(类型X),并且在多个线程中使用它,则不会导致任何问题(例如,由于X的公共方法受到保护/同步)。

Saying for a thread/process that it's thread safe or not - that doesn't make much sense, it is about classes and objects of these classes. 说一个线程/进程是否是线程安全的-没什么意义,它与这些类的类和对象有关。

When a class is not thread safe you can use objects of this class as local variables and not worry about thread-safety too. 当一个类不是线程安全的时,您可以将该类的对象用作局部变量,而不必担心线程安全。

That's because a local variable is not shared across threads, each thread has its own copy of it. 这是因为局部变量不跨线程共享,每个线程都有它自己的副本。

But if you use it as class variable you can have thread safety problems. 但是,如果将其用作类变量,则可能会出现线程安全问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM