Results 1 to 4 of 4
  1. #1
    darkgoku is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default Multithreading to improve execution time

    Dear Java experts,

    I did my Java course at school many years ago, and I am posed with an issue I am trying to solve.

    I have a main method, which in it I have two custom java objects I have created.
    Each of these objects are independent of one another, and each take a long time to initialise, which is due to an external api call.
    What Im trying to do, is put the initialisation of these objects, each on a separate thread, and then join the threads in the end.
    This is what Ive tried so far:

    Java Code:
     
             YelpInfo ylp;
    		ArrayList<User.FSItem> fs;
    
    		Thread yelpThread = new Thread() {
    		    public void run() {
    				ylp = getYelpForUser(userLoc, cat, milesRadius);
    		    }
    		};
    
    		Thread foursquareThread = new Thread() {
    		    public void run() {
    				fs = getFoursquareForUser(userLoc, cat, milesRadius);
    		    }
    		};
    
    		yelpThread.start();
    		foursquareThread.start();
    
    		// Wait for them all to finish
    		yelpThread.join();
    		foursquareThread.join();
    Yet remains the issues:
    Cannot refer to a non-final variable inside an inner class defined in a different
    method
    Cannot refer to a non-final variable ylp inside an inner class defined in a different
    method

    Any advice on how to achieve this properly?

    Thanks so much.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,718
    Rep Power
    5

    Default Re: Multithreading to improve execution time

    Don't use anonymous inner classes; make the threads proper classes on their own and put the ylp and fs as attributes of those individual classes.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Multithreading to improve execution time

    A couple of other items.

    You are getting that error because you are referencing a local variable in an anonymous class. If those variables were instance fields of the containing class it would go away. And you need to put your join calls in a try/catch block if not already done.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    darkgoku is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default Re: Multithreading to improve execution time

    Thank you for your replies.
    Both of them were valid, and worked perfectly.

Similar Threads

  1. Best way to Time Execution
    By tabchas in forum New To Java
    Replies: 2
    Last Post: 06-13-2011, 09:20 AM
  2. improve web server startup-time
    By ideyatech in forum Java Software
    Replies: 1
    Last Post: 04-22-2010, 04:16 AM
  3. Replies: 2
    Last Post: 12-29-2009, 11:53 AM
  4. Improve the slow running time
    By coolFrenzi in forum Advanced Java
    Replies: 1
    Last Post: 04-12-2009, 12:15 PM
  5. Replies: 25
    Last Post: 06-28-2008, 05:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •