Redian新闻
>
问个HttpClient 的问题
avatar
问个HttpClient 的问题# Java - 爪哇娇娃
s*d
1
第一次用http protocol, 不太熟,想问一下,我写的是client side, 如果我需要两个
thread, thread1 is just doing regular basis GET request and read the
response in a while loop. Thread2 needs to send GET request once and keep
the connection open and read the response stream continuously.(别问为什么,
不是我design的) Do I need two HttpClient instances? Because I'm afraid if I
only create one HttpClient, it will reuse the connection, then thread1 will
be blocked by thread2 since it never ends reading the data. If I create two
HttpClient
avatar
a*i
2
If I were you, I will use two instances of HttpClient.
what you need be aware of depends on the data you are going to
send and receive, I guess
in short, be careful of the stuffs need be synchronized

I
will
two
connection

【在 s******d 的大作中提到】
: 第一次用http protocol, 不太熟,想问一下,我写的是client side, 如果我需要两个
: thread, thread1 is just doing regular basis GET request and read the
: response in a while loop. Thread2 needs to send GET request once and keep
: the connection open and read the response stream continuously.(别问为什么,
: 不是我design的) Do I need two HttpClient instances? Because I'm afraid if I
: only create one HttpClient, it will reuse the connection, then thread1 will
: be blocked by thread2 since it never ends reading the data. If I create two
: HttpClient

avatar
s*d
3
thanks, I'll try when the backend is ready. Data synchronization should not
be a concern so far, I only worry about sending/receiving data through
separate threads will interfere each other.
So can I assume that each HttpClient will establish one http connection?

【在 a****i 的大作中提到】
: If I were you, I will use two instances of HttpClient.
: what you need be aware of depends on the data you are going to
: send and receive, I guess
: in short, be careful of the stuffs need be synchronized
:
: I
: will
: two
: connection

avatar
m*t
4
I don't see why you can't use MultiThreadedConnectionManager with
different configurations.

not

【在 s******d 的大作中提到】
: thanks, I'll try when the backend is ready. Data synchronization should not
: be a concern so far, I only worry about sending/receiving data through
: separate threads will interfere each other.
: So can I assume that each HttpClient will establish one http connection?

avatar
s*d
5
Ok, I see some sample code like the following. So if I do this and use one
instance of httpClient for both threads, after both threads sends GET
requests, one thread is reading responds, the other one won't be blocked?
releasing the connection in one thread will not affect other threads?
public void createHttpClient() {
connectionManager = new MultiThreadedHttpConnectionManager();
final HttpConnectionManagerParams cmp = connectionManager.getParams();
cmp.setStaleCheckingEnabled(staleCheckingEnab

【在 m******t 的大作中提到】
: I don't see why you can't use MultiThreadedConnectionManager with
: different configurations.
:
: not

avatar
m*t
6
I think this looks fine. You'll have to try it yourself though.

【在 s******d 的大作中提到】
: Ok, I see some sample code like the following. So if I do this and use one
: instance of httpClient for both threads, after both threads sends GET
: requests, one thread is reading responds, the other one won't be blocked?
: releasing the connection in one thread will not affect other threads?
: public void createHttpClient() {
: connectionManager = new MultiThreadedHttpConnectionManager();
: final HttpConnectionManagerParams cmp = connectionManager.getParams();
: cmp.setStaleCheckingEnabled(staleCheckingEnab

avatar
a*i
7
why bother? I think that just likes you open two browsers

【在 s******d 的大作中提到】
: Ok, I see some sample code like the following. So if I do this and use one
: instance of httpClient for both threads, after both threads sends GET
: requests, one thread is reading responds, the other one won't be blocked?
: releasing the connection in one thread will not affect other threads?
: public void createHttpClient() {
: connectionManager = new MultiThreadedHttpConnectionManager();
: final HttpConnectionManagerParams cmp = connectionManager.getParams();
: cmp.setStaleCheckingEnabled(staleCheckingEnab

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。