java多线程发请求
时间: 2024-06-13 21:02:06 浏览: 6
在Java中,多线程并发地发送网络请求通常用于提高性能和并行处理多个任务。你可以使用`ExecutorService`、`Thread`、`Callable`和`Future`等Java并发工具来实现这个功能。以下是一个简单的示例:
```java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.net.HttpURLConnection;
import java.io.OutputStream;
import java.io.InputStreamReader;
public class MultiThreadingExample {
private static final int THREAD_COUNT = 5; // 线程数量
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(THREAD_COUNT);
for (int i = 0; i < THREAD_COUNT; i++) {
int taskNumber = i;
executor.submit(new HttpRequestTask("http://example.com/task" + taskNumber));
}
executor.shutdown(); // 关闭线程池,等待所有任务完成
while (!executor.isTerminated()) { // 如果有任务仍在执行,阻塞直到它们完成
}
System.out.println("All tasks completed.");
}
static class HttpRequestTask implements Runnable {
private String url;
public HttpRequestTask(String url) {
this.url = url;
}
@Override
public void run() {
try {
sendRequest(url);
System.out.printf("Task %d: Request to %s sent%n", Thread.currentThread().getId(), url);
} catch (Exception e) {
System.out.printf("Task %d: Error sending request to %s: %s%n", Thread.currentThread().getId(), url, e.getMessage());
}
}
private void sendRequest(String url) throws Exception {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
try (OutputStream outputStream = connection.getOutputStream()) {
outputStream.write("Hello, Server!".getBytes()); // 示例,实际请求可能包含更复杂的逻辑
}
try (InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream())) {
String response = inputStreamReader.readAllLines().get(0); // 读取响应
System.out.printf("Task %d: Received response: %s%n", Thread.currentThread().getId(), response);
}
} else {
throw new RuntimeException("Unexpected response code: " + responseCode);
}
}
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)