CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Tiko hakobyan
Level 20
Yerevan
  • 04.07.2020
  • 416views
  • 4comments

am i missing something? first requirement wont pass.

Question about the task One for all, all for one
Java Core,  Level 6,  Lesson 10
Resolved

1. Figure out how the program works.
1.1. Note that the Water object is the same for all the threads.
2. Implement the ourInterrupt() method so that it interrupts all threads in the threads list.
3. Correct the values ​​of the variables in the run() method:
3.1. isCurrentThreadInterrupted must be the same as the value of the isInterrupted() method for the current thread.
3.2. threadName must be the same as the value of the getName() method (implemented in the Thread class) for the current thread.

Requirements:
  • The ourInterrupt() method must interrupt all threads in the threads list.
  • The run() method must get the current thread using Thread.currentThread.
  • The run() method must call the isInterrupted() method on the current thread.
  • The run() method must call the getName() method on the current thread.
  • The main() method should run for about 3 seconds.
package com.codegym.task.task16.task1620; import java.util.ArrayList; import java.util.List; /* One for all, all for one */ public class Solution { public static byte countThreads = 3; static List<Thread> threads = new ArrayList<>(countThreads); public static void main(String[] args) throws InterruptedException { initThreadsAndStart(); Thread.sleep(3000); ourInterrupt(); } public static void ourInterrupt() { //write your code here for (int i = 0; i < threads.size(); i++) { threads.get(i).interrupt(); } } private static void initThreadsAndStart() { Water water = new Water("water"); for (int i = 0; i < threads.size(); i++) { threads.add(new Thread(water, "#" + i)); } for (int i = 0; i < threads.size(); i++) { threads.get(i).start(); } } public static class Water implements Runnable { private String sharedResource; public Water(String sharedResource) { this.sharedResource = sharedResource; } public void run() { //fix 2 variables boolean isCurrentThreadInterrupted = Thread.currentThread().isInterrupted(); String threadName = Thread.currentThread().getName(); try { while (!isCurrentThreadInterrupted) { System.out.println("Object " + sharedResource + ", thread " + threadName); Thread.sleep(1000); } } catch (InterruptedException e) { } } } }
+2
Comments (4)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Thomas
Level 33 , Bayreuth, Germany
4 July 2020, 13:28
strange, this should work. The below code validated for me. Maybe you want to try it and see if it's your loop or something else.
for (Thread t : threads)
    t.interrupt();
0
Tiko hakobyan
Level 20 , Yerevan, Armenia
6 July 2020, 12:32
thanks for the replay i've tried enhanced for loop which did not pass the validation as well
0
Thomas
Level 33 , Bayreuth, Germany
6 July 2020, 13:03solution
I just see, that you modified the given code. Maybe that's a problem for validation. I'd reset the task and retry. Other than that I can't spot any problem.
+2
Tiko hakobyan
Level 20 , Yerevan, Armenia
6 July 2020, 13:18
thank you very much, looks like i have deleted that part of the code for some reason i cant recall at the moment and tried to wright it down from memory after that.
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 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.