0

I am trying execute two jobs simultaneously. One of the things that I am trying to do is displaying a count up timer and the other one is moving the ball.

This is where I create the timer and also call the moveBall method

    button.addChangeListener(new ChangeListener() {

        int start = 0;

        public void stateChanged(ChangeEvent e) {

            ActionListener taskPerformer = new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    timeValue.setText(++start + " sec");
                }
            };

            timer = new Timer(1000, taskPerformer);
            timer.start();

            ball.moveBall();

        }
    });

This is my moveBall method

public void moveBall() {

    Thread ball = new Thread() {

        double counter = 0;

        int t = (int) (2 * Vy / 9.8);  

        public void run() {
            try {
                while (t >= 0) {
                    // calculate Vx and Vy

                    Ball.this.setX(Ball.this.getX() + Vx);
                    Ball.this.setY(Ball.this.getY() - Vy);

                    counter += 50;

                    if (counter == 1000) {
                        t--;
                        counter = 0;
                    }

                    paintingComponent.repaint();

                    Thread.sleep(20);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };

    ball.start();
}

When I execute the above code the label for displaying the time passed is not changing at all during the ball is moving and when the movement is over it takes the last number that it supposed to take.

Hunk
  • 43
  • 4
  • You should use the Swing Timer to update both. You can actually create multiple Swing Timers if that's easier for you – ControlAltDel May 03 '16 at 16:15
  • Does the label change its value if you do not start the "ball-thread"? – Supahupe May 03 '16 at 16:16
  • Yes it does change its value when I don't start the ball-thread. – Hunk May 03 '16 at 16:18
  • This sounds like an problem of the Event Dispatch Thread. Im not sure, but maybe you find an answer here: http://stackoverflow.com/questions/7896723/how-do-you-use-the-event-dispatch-thread . You should only make GUI-modifications from the Event Dispatch Thread. I think this is the problem because your Thread is in another Thread Pool and blocks the GUI when you call the repaint()-method – Supahupe May 03 '16 at 16:26

1 Answers1

0

This is a example two executions of two threads, Java simultaneous execution

public class ThreadExecutor extends Thread {

    private String name;

    private int executionCount;

    public ThreadExecutor(final String name, final int executionCount) {
        this.name = name;
        this.executionCount = executionCount;
    }

    @Override
    public void run() {

        int count = 1;

        while (count <= executionCount) {
            System.out.println("Executing thread ".concat(name).concat(" : ") + count);
            count++;
        }

     }
}

    public class Main {

        public static void main(String args[]) {

            final ThreadExecutor one = new ThreadExecutor("One", 1);
            final ThreadExecutor two = new ThreadExecutor("Two", 2);

            one.start();
            two.start();
        }
}
Harold Castillo
  • 2,006
  • 19
  • 23
  • It increases the value of the second parameter of the constructor to 100 and 10 respectively and see the concurrence. – Harold Castillo May 03 '16 at 16:57
  • Executing thread One : 1 Executing thread Two : 1 Executing thread One : 2 Executing thread Two : 2 Executing thread One : 3 Executing thread Two : 3 Executing thread One : 4 Executing thread Two : 4 Executing thread One : 5 Executing thread Two : 5 Executing thread One : 6 Executing thread One : 7 Executing thread One : 8 Executing thread One : 9 Executing thread One : 10 Executing thread One : 11 Executing thread One : 12 Executing thread One : 13 Executing thread One : 14 Executing thread One : 15 ... – Harold Castillo May 03 '16 at 17:35