CodeGym
促销活动
CodeGym University
学习
课程
任务
调查和小测验
游戏
帮助
提醒时间表
社区
用户
论坛
聊天
文章
成功故事
活动
评论
订阅服务
浅色主题
问题
  • 评论
  • 关于我们
开始
开始学习
立即开始学习
  • 全部问题
null
第 20 级
Beijing
  • 16.08.2020
  • 417次阅读
  • 1个评论

Help

有关 任务的问题 早起的鸟儿有虫吃
Java 核心,  第 6 级,  课程 13
正在讨论

1.弄明白程序的功能。
1.1.每个线程应从控制台读取几行内容。使用现有的 static BufferedReader reader。
1.2.使用 AtomicInteger readStringCount 计算所有线程从控制台读取多少行内容。
2.实现 run 方法:
2.1.只要线程未中断 (!IsInterrupted),从控制台读取几行内容,然后将其添加到 List<String> result。
2.2.使用 readStringCount 对从控制台读取的字符串计数。

要求:
  • run 方法必须运行到线程终止 (!IsInterrupted) 为止。
  • run 方法不得创建其自己的 InputStreamReaders 或 BufferedReaders。
  • run 方法必须从读取器读取字词,然后将其添加到 result 列表。
  • 读取每个字符串后,run 方法必须对 readStringCount 加 1。
  • 程序应显示每个线程读取的数据。
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(); } } }
0
评论 (1)
  • 受欢迎
  • 新
  • 旧
你必须先登录才能发表评论
zhs
级别 18 ,China
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
了解
  • 注册
  • Java 课程
  • 任务帮助
  • 定价
  • 游戏项目
  • Java 语法
社区
  • 用户
  • 文章
  • 论坛
  • 聊天
  • 成功故事
  • 活动
  • Affiliate Program
公司
  • 关于我们
  • 联系方式
  • 评论
  • 新闻发布室
  • CodeGym 教育
  • 常见问题
  • 支持
CodeGym CodeGym 是一个从零开始学习 Java 语言编程的在线课程。本课程是初学者掌握 Java 语言的绝佳方式。它包含 1200 多个可即时验证的任务,以及基本范围内的 Java 基础理论。为了帮助你在教育上取得成功,我们实现了一组激励功能:小测验、编码项目以及有关高效学习和 Java 语言开发人员职业方面的内容。
关注我们
界面语言
程序员不是天生的 © 2023 CodeGym
MastercardVisa
程序员不是天生的 © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.