CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
Niladri pradhan
Level 33
Mumbai
  • 15.12.2019
  • 729views
  • 1comment

Last two condition not met

Question about the task Plants vs Zombies
Java Multithreading,  Level 8,  Lesson 10
Resolved

1. Read about java.util.concurrent.Phaser.
2. Put the methods in the right places:
- arriveAndDeregister(): starts calling the tasks' run methods,
- arriveAndAwaitAdvance(): waits for all threads to be created.

See output.txt for example output.
Logic:
First, all the characters join the game.
After all the characters have joined the game and are ready to play, the phrase "The game has begun!" is displayed.
After that, the characters enter the game and then die.

Requirements:
  • Don't change the Character class.
  • Don't change the Plant and Zombie classes.
  • In the Solution class, call phaser.arriveAndDeregister in the correct place.
  • In the Solution class, call phaser.arriveAndAwaitAdvance in the correct place.
package com.codegym.task.task28.task2809; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Phaser; /* Plants vs Zombies */ public class Solution { public static void main(String[] args) throws InterruptedException { List<Character> characters = new ArrayList<>(); characters.add(new Plant()); characters.add(new Plant()); characters.add(new Zombie()); characters.add(new Zombie()); characters.add(new Zombie()); start(characters); } private static boolean isEveryoneReady = false; private static void start(List<Character> characters) throws InterruptedException { final Phaser phaser = new Phaser(1 + characters.size()); phaser.arriveAndDeregister(); for (final Character character : characters) { final String member = character.toString(); System.out.println(member + " joined the game"); new Thread() { @Override public void run() { System.out.println(member + " is preparing to play"); phaser.arriveAndAwaitAdvance(); if (!isEveryoneReady) { phaser.arriveAndDeregister(); isEveryoneReady = true; System.out.println("The game has begun!"); } character.run(); } }.start(); } } }
0
Comments (1)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Wei Cui
Level 39 , Newark, United States
8 April 2020, 01:56
add phaser.arriveAndDeregister(); between line 44 and line 45
+2
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.