Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) {
- Time time = new Time();
- for (int i = 0; i < 10; i++) {
- new Ball(Math.random(), i * 10, time);
- }
- while (true) {
- time.addTime();
- if (time.getTime() == 100) {
- break;
- }
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- class Ball implements Runnable {
- private int x = 0;
- private int y = 0;
- private double speed;
- private int duration;
- private Time time;
- private boolean finished;
- public Ball(double speed, int duration, Time time) {
- this.speed = speed;
- this.duration = duration;
- this.time = time;
- new Thread(this).start();
- }
- public void move(int x, int y) {
- this.x += x * speed;
- this.y += y * speed;
- }
- @Override
- public void run() {
- while (true) {
- synchronized (time) {
- if (time.getTime() >= duration) {
- finished = true;
- break;
- }
- }
- }
- System.out.println("Ball finished");
- }
- }
- class Time {
- private int time = 0;
- public void addTime() {
- time += 1;
- }
- public int getTime() {
- return time;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement