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 2 of 2

Thread: adding 2 random numbers using multi threading

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default adding 2 random numbers using multi threading

    I am attempting to add two numbers together using 3 concurrent thread objects in one class. I have implemented the runnable interface and used the run() method. The error I am receiving states that my "class is not abstract and does not override abstract method run() in runnable.

    very basic when it comes to threads. any help greatly appreciated.

    //import packages
    import java.util.*; 
     
    //random thread class
    public class RandomThread implements Runnable
    {
        //integer and object variables
        Random rNum1 = new Random();
        Random rNum2 = new Random();
        int num1, num2;
     
       RandomThread()
       {
            //chose random first number
            Thread thNum1 = new Thread()
            {
                //run method from runnable interface
                public void run()
                {
                    for (int i = 0; i <= 10; i++)
                    {
                        num1 = rNum1.nextInt(11);
                    }
                }
            };
            thNum1.start();
     
            //chose random second number
            Thread thNum2 = new Thread()
            {
                //run method from runnable interface
                public void run()
                {
                    for (int i = 0; i <= 10; i++)
                    {
                        num2 = rNum2.nextInt(11);
                    }
                }
            };
            thNum2.start();
     
            //chose random second number
            Thread thSum = new Thread()
            {
                //run method from runnable interface
                public void run()
                {
                //sum of 2 random numbers
                int sum = num1 + num2;
                System.out.println("The sum of " + num1 + " +" + num2 + " =" + sum);
                }
            };
            thSum.start();
       }
     
       //main method
       public static void main(String[] arguement)
       {
           //instance of class
           RandomThread rt = new RandomThread();
       }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,447
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: adding 2 random numbers using multi threading

    First off, why are you using threads? That's not a good way to use them, and it seems like you might be getting into them before you really understand the basics.

    But your error says it all. Your RandomThread class implements Runnable for some reason. Anything that implements Runnable has to have a run() method. You don't have a run() method in your RandomThread class, just the run methods inside your anonymous inner Thread classes.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 12
    Last Post: May 1st, 2013, 02:55 PM
  2. Replies: 7
    Last Post: April 27th, 2013, 07:15 AM
  3. InterruptedException in multi threading
    By me_shankara in forum Threads
    Replies: 1
    Last Post: April 18th, 2013, 12:54 PM
  4. Replies: 2
    Last Post: January 6th, 2012, 09:50 PM
  5. Replies: 1
    Last Post: March 23rd, 2010, 02:29 AM