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

Thread: Objects communication issue

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Objects communication issue

    Hello. I'm new in OOP so this might have an easy answer.

    I have an abstract class with a lot of functionality. The functionality I'm interested in are 2 methods.
    One public object[] getF(object[]) that takes an array of objects, simulates all of them multithreaded and returns the same array objects with the results in their properties.
    And one other public object getF(object) that takes only one object of the same type, makes it an array[1] and send it to the first method. It also returns the same object with the result.

    I have 2 inherited classes of the abstract class. The first takes some properties, builds an object of the 2nd class and gets the results from it.
    The 2nd class is an optimization algorithm which builds an object and sends it to the getF(object) method of the abstract class. Takes back the result, calculates the next one etc until it finishes and sends the result to the 1st class.

    What I want to do is to build in the 1st class (or a new one) that builds more than one objects of the 2nd class. When I do this, every object of the 2nd class simulates onethreaded because every one of them calls getF(object). I want them to call a getF method which waits until it gets object from every 2nd class object and then send them to getF(object[]) for multithreaded operation. When it finishes it should return the results to the correct 2nd class object.

    How can I do this?


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

    Default Re: Objects communication issue

    I'm a bit confused by your description. Do you think you could put together an SSCCE that demonstrates what you're talking about? It should be as short as possible, so use dummy methods instead of methods that do anything (except the methods that matter).
    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!

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Objects communication issue

    Ok, Ill try again.

    public abstract class optimizer{
    public point[] getF(point[] a_points){ calculates all points multithreaded}
    public point getF(point a_point){ makes an array of one point[] and calls getF(point[])}
    }

    public class optimizerAlgorithm extends optimizer{
    public point run(){..........
    resultPoint = getF(point);
    .....................
    return resultPoint;
    }

    public class optimizerRun extends optimizer{
    public void main(....){
    optimizerAlgorithm alg1 = new optimizerAlgorithm();
    optimizerAlgorithm alg2 = new optimizerAlgorithm();

    point result1 = alg1.run();
    point result2 = alg2.run();
    ....do something with them.....exit}

    Sorry about spelling issues but I think you get the philosophy.
    The above works just fine, but one threaded because every object calls get(point).
    How can I get all points from alg objects and call getF(point[]) to do this multithreaded. And then how I return those values to their alg objects?

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

    Default Re: Objects communication issue

    Sorry, I'm still confused (that's more pseudocode than an SSCCE). What about multithreading is giving you trouble?
    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!

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Objects communication issue

    every alg object is calling the one threaded version of getF(point). That's normal.
    If I want it to be multithreaded, I should have an object which gathers every point from alg objects when they call getF(point) and when the points are equal to the number of alg objects, then they will be put in an array point[] and send to getF(point[]) for multithreaded calculation.
    But when I get the results, how I can send them back to the alg objects so they will continue their calculations from where they stop when they called getF(point).
    Sorry if I'm not clear and thanks for the quick reply.

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Objects communication issue

    One other thing to consider is that the multithreaded implementation works well in getF(point[]), so I don't have to consider about multithreaded operation. I just have to send all points together.

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

    Default Re: Objects communication issue

    So your real question is how to wait for all threads to finish execution before returning anything, and it has nothing to do with abstract classes or Objects?

    If so, you might want to look at thread pools, or simply the Thread.join() function.
    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!

  8. #8
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Objects communication issue

    The thread waiting has been implemented in getF(point[]) method.
    Every alg object sends one point to getF(point) and waits the return result.
    How can I gather all points from every alg object? If I do this, then I'll send all of them as an array to getF(point[]) and put the result to the corresponting variable. After doing this how can I return the values back to the alg objects so they can continue from their stoping point?

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

    Default Re: Objects communication issue

    I guess you'd just iterate over every alg (do you mean Alg?) Object, getting the Points from each and collecting them in an array or List. Then do whatever you want with them.

    If that doesn't answer your question, you're really going to have to throw together an SSCCE- that's not your whole program, just a few lines of real, compilable, runnable code that demonstrates what you're actually talking about.
    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!

  10. #10
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Objects communication issue

    Ok we are going somewhere. This is the half of the solution I'm looking.
    After I collect the points I send them to getF(point[]) and collect the results.
    Ok so far.

    Every alg object which originaly called getF(point) waits for return point. How can I return the apropriate value to the algs.
    If I had only one alg object called getF(point) then I just return the value.
    Now that I have more than one what can I do?

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

    Default Re: Objects communication issue

    You iterate over them. Like I said, if you still have questions, we really need to see an SSCCE as I'm mostly just guessing at what you're talking about here.
    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. Network communication hangs
    By droyhull in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 8th, 2011, 10:55 AM
  2. server/client communication problem
    By perl0101 in forum Java Networking
    Replies: 8
    Last Post: May 24th, 2011, 01:58 PM
  3. client server communication
    By Brt93yoda in forum Java Theory & Questions
    Replies: 4
    Last Post: September 2nd, 2010, 04:49 PM
  4. communication protocol
    By isaac in forum Java Networking
    Replies: 1
    Last Post: February 5th, 2010, 08:20 AM
  5. servlet applet communication
    By prashanthi_asn in forum Java Servlet
    Replies: 0
    Last Post: May 21st, 2009, 12:50 AM