此次提交的代码展示了一个简单的字节流处理示例,以及注释掉的并发处理代码。主要目的是通过ByteArrayInputStream从字符串构建字节流,然后使用循环读取字节流并打印出来。虽然提交的代码不包含实际的线程池使用,但通过注释可以看出,原先可能存在对线程池的测试代码。此代码片段作为一个基础,可能用于测试或演示Java中的IO处理和并发编程。

This commit is contained in:
kyriewhluo 2024-08-06 10:19:27 +08:00
parent 536890ab62
commit 5a11845534

View File

@ -0,0 +1,48 @@
package cn.whaifree.test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestCacheThreadPool {
public static void main(String[] args) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 20; i++) {
stringBuilder.append(i);
}
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(stringBuilder.toString().getBytes(Charset.defaultCharset()));
byte[] buffer = new byte[10];
int length = 0;
while ((length = byteArrayInputStream.read(buffer)) != -1) {
System.out.println(new String(buffer, 0, length));
System.out.println("\n");
}
// ExecutorService executorService = Executors.newCachedThreadPool();
// for (int i = 0; i < 10; i++) {
// int finalI = i;
// executorService.submit(new Runnable() {
// @Override
// public void run() {
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// }
//
// System.out.println(finalI
// +" "+Thread.currentThread().getName());
// }
// });
// }
}
}