/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package mt; /** * * @author (C)Y.D.Zakovryashin, 09.01.2023 */ public class PingPong implements Runnable { public static final int ROUND = 10; private String name; private int delay; private Ball ball; public PingPong(String name, int delay, Ball ball) { this.name = name; this.delay = delay; this.ball = ball; } public static void main(String[] args) throws InterruptedException { Ball ball = new Ball(); PingPong gamer1 = new PingPong("ping", 300, ball); PingPong gamer2 = new PingPong("pong", 500, ball); Thread t1 = new Thread(gamer1); Thread t2 = new Thread(gamer2); System.out.println("Start game..."); t1.start(); t2.start(); try { t1.join(8000); t2.join(8000); synchronized (ball) { ball.notifyAll(); } } catch (InterruptedException e) { System.out.println("Error: " + e.getMessage()); } System.out.println("End over."); } @Override public void run() { for (int i = 0; i < ROUND; ++i) { ball.hit(name, delay); } } }