package zh.codegym.task.task16.task1628;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class Solution {
public static volatile AtomicInteger readStringCount = new AtomicInteger(0);
public static volatile BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
// 读取字符串计数
int count = Integer.parseInt(reader.readLine());
// 初始化线程
ReaderThread consoleReader1 = new ReaderThread();
ReaderThread consoleReader2 = new ReaderThread();
ReaderThread consoleReader3 = new ReaderThread();
consoleReader1.start();
consoleReader2.start();
consoleReader3.start();
while (count > readStringCount.get()) {
}
consoleReader1.interrupt();
consoleReader2.interrupt();
consoleReader3.interrupt();
System.out.println("#1:" + consoleReader1);
System.out.println("#2:" + consoleReader2);
System.out.println("#3:" + consoleReader3);
reader.close();
}
public static class ReaderThread extends Thread {
private List<String> result = new ArrayList<>();
public void run() {
//在此编写你的代码
try
{
while(!Thread.currentThread().isInterrupted())
{
String str = reader.readLine();
if(!str.isEmpty())
{
result.add(str);
readStringCount.getAndAdd(1);
}
else
{
break;
}
}
}
catch(Exception e)
{
//e.printStackTrace();
}
}
@Override
public String toString() {
return result.toString();
}
}
}
Help
正在讨论
评论 (1)
- 受欢迎
- 新
- 旧
你必须先登录才能发表评论
zhs
30 十月 2020, 03:26
需要判空:
while (!isInterrupted()) {
String str = null;
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (str !=null) {
result.add(str);
readStringCount.getAndAdd(1);
}
}
0