Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Can't simultaneously run two threads??

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Exclamation Can't simultaneously run two threads??

    Hey Guys,

    I am creating a program which creates two balls (each with their own runnables which control them). This is the important code from the file:

    public BallsFrame() {
            setSize(FRAME_WIDTH, FRAME_HEIGHT);
     
            addBall(150,50);
            addBall(200,100);
     
    // add two balls to pane
     
        }
    // addBall to panel at specified coordinates (x,y)
        public void addBall(int x, int y) {
    // create coloured ball and add to panel
            BallComponent ball = new BallComponent(x, y);
            add(ball);
    // create a runnable to control ball
            Runnable r = new ColoredBallRunnable(ball);
            Thread t = new Thread(r);
            System.out.println("addBall t started...");
            t.start();
            System.out.println("addBall t finished...");
        }

    The ControlBallRunnable is here:

    public class ColoredBallRunnable implements Runnable {
        BallComponent ball;
        ColoredBallPanel cbPanel;
        public ColoredBallRunnable(BallComponent ball){//, ColoredBallPanel cbPanel){
            this.ball = ball;
            this.cbPanel = cbPanel;
        }
     
        public void run() {
            class TimerListener implements ActionListener {
     
                public void actionPerformed(ActionEvent event) {
                    ball.animate();
                }
            }
            TimerListener listener = new TimerListener();
            Timer t = new Timer(1000, listener);
            t.start();
        }
     
    }
    To explain this: when the runnable is created, it creates a timer which changes the colour of the ball (using method ball.animate() ) every 1000ms i.e. every second.

    Now my problem is this code partially works, it successfully creates one ball (the first time addBall() is called).
    It refuses to create more than one ball.

    Now, ive researched this problem for the last couple of days and cant seem to find a solution. Please help me. If you need any clarifications, extra code etc, i will provide it.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Can't simultaneously run two threads??

    For testing this kind of problem you need a program that compiles, executes and shows the problem.
    Please make a small program for testing and post it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Can't simultaneously run two threads??

    Quote Originally Posted by Norm View Post
    For testing this kind of problem you need a program that compiles, executes and shows the problem.
    Please make a small program for testing and post it.
    Hi i tried to make a small version of the program but i couldnt change classes into methods etc, as i am quite new at java. Heres a project folder from netbeans which contains all the source.
    Sorry for the inconvenience caused, but this is the closest i can get. Thanks

    http://www.filedropper.com/ballsthreads

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Can't simultaneously run two threads??

    Please post the code here on the forum.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Can't simultaneously run two threads??

    Sorry about that. Here is the various pages of the program:

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Can't simultaneously run two threads??

    Sorry, having 5 separate files makes it hard to copy for testing.
    Can you put all the source into a single file to make it easier to copy and paste into my editor for testing?

    I got it.

    I get this error:
    cannot find symbol
    symbol : class ColoredBallPanel
    Now my problem is this code partially works, it successfully creates one ball (the first time addBall() is called).
    It refuses to create more than one ball.
    What is supposed to cause it to create a second ball?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Can't simultaneously run two threads??

    Hi i think i found out where i was going wrong. I was adding the component directly to the frame (refer to BallsFrame.java). I think you can only add one component directly to a frame.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Can't simultaneously run two threads??

    Yes that's it. Make these changes to see:
            setSize(FRAME_WIDTH, FRAME_HEIGHT);
            setLayout(new GridLayout(4,1)); //<<<<<<<<<<<<<<<< ADDED
     
            addBall(10,50);
            addBall(200,45);
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Can't simultaneously run two threads??

    It works!!! BTW i was trying out FlowLayout before you suggested GridLayout, and none of the components were showing on the frame. Do you have any idea why?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Can't simultaneously run two threads??

    I have no ideas.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Can't simultaneously run two threads??

    Quote Originally Posted by Norm View Post
    I have no ideas.
    If i can ask one last question, i am trying to make a frame full of bouncing balls, i.e. each ball controlled by their own runnable, which bounce off the window panes. What would be a good layout manager to use in that instance? Can i use gridlayout still?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Can't simultaneously run two threads??

    Change the logic to have a single component with an override to the paintComponent() method that has a list of balls to draw within its area.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Can't simultaneously run two threads??

    Quote Originally Posted by Norm View Post
    Change the logic to have a single component with an override to the paintComponent() method that has a list of balls to draw within its area.
    Unfortunately thats a pre-requisite that i have to have each ball controlled by its own runnable/thread,

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Can't simultaneously run two threads??

    A thread is not a component. You can have many threads used by or using one component. By component I mean a class that extends the Component class, like JPanel or JButton.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Can't simultaneously run two threads??

    Sorry i dont understand how i can override paintcomponent from within the thread, surely you have to set public methods and variables within the paintcomponent to modify it from an altogether different class, the BallsFrame class?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Can't simultaneously run two threads??

    You define a component like JPanel and override its paintComponent() method. It will be called when the JPanel's repaint() method is called. There will be a list of the Balls to draw that the paintComponent() method will use to draw the balls.

    What are the threads supposed to do?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  2. Simultaneously running statements
    By CrashOverride in forum Java Theory & Questions
    Replies: 2
    Last Post: October 19th, 2012, 03:32 PM
  3. Threads priority: low priority run faster than high priority
    By leonixyz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 8th, 2011, 04:33 PM
  4. How to use PrintSteam and BufferedReader Simultaneously
    By bigmac025 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 29th, 2011, 08:30 AM
  5. How Can I run java threads
    By Saeid in forum Threads
    Replies: 12
    Last Post: August 6th, 2010, 03:12 PM