Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. public class Main {
  2.     public static void main(String[] args) {
  3.         Time time = new Time();
  4.  
  5.         for (int i = 0; i < 10; i++) {
  6.             new Ball(Math.random(), i * 10, time);
  7.         }
  8.  
  9.         while (true) {
  10.             time.addTime();
  11.  
  12.             if (time.getTime() == 100) {
  13.                 break;
  14.             }
  15.  
  16.             try {
  17.                 Thread.sleep(100);
  18.             } catch (InterruptedException e) {
  19.                 e.printStackTrace();
  20.             }
  21.         }
  22.     }
  23. }
  24.  
  25. class Ball implements Runnable {
  26.     private int x = 0;
  27.     private int y = 0;
  28.     private double speed;
  29.     private int duration;
  30.     private Time time;
  31.  
  32.     private boolean finished;
  33.  
  34.     public Ball(double speed, int duration, Time time) {
  35.         this.speed = speed;
  36.         this.duration = duration;
  37.         this.time = time;
  38.  
  39.         new Thread(this).start();
  40.     }
  41.  
  42.     public void move(int x, int y) {
  43.         this.x += x * speed;
  44.         this.y += y * speed;
  45.     }
  46.  
  47.     @Override
  48.     public void run() {
  49.         while (true) {
  50.             synchronized (time) {
  51.                 if (time.getTime() >= duration) {
  52.                     finished = true;
  53.                     break;
  54.                 }
  55.             }
  56.         }
  57.  
  58.         System.out.println("Ball finished");
  59.     }
  60. }
  61.  
  62. class Time {
  63.     private int time = 0;
  64.  
  65.     public void addTime() {
  66.         time += 1;
  67.     }
  68.  
  69.     public int getTime() {
  70.         return time;
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement