Results 1 to 4 of 4
  1. #1
    Brad is offline Member
    Join Date
    Feb 2012
    Posts
    21
    Rep Power
    0

    Default Help with "implements Runnable"

    I am pretty new to Java and the more I read about threads, the more I start to get confused. I have a method that does a database task that can sometimes take awhile. When it does this my GUI locks up until the task is done. So I figured running this on a separate thread would make sense.

    Here is what I have in my class

    Java Code:
    public class BucketTool implements Runnable{
      private String bucketPath;
      private String user;
      private String pass;
      private String dbset;
      private String dbname;
      private String qr;
     
        public void bucket(String bucketPath, String user, String pass, String dbset, String dbname, String qr) {
            this.bucketPath = bucketPath;
            this.user = user;
            this.pass = pass;
            this.dbset = dbset;
            this.dbname = dbname;
            this.qr = qr;
    
           public void bucket(String bucketPath, String user, String pass, String dbset, String dbname, String qr) {
            
                       .....some method logic here
         }
        @Override
          public void run(){
          bucket(bucketPath, user, pass, dbset,dbname,qr);   
       }
      }
    Then I have the following code in Main

    Java Code:
    //        if(fail ==0){
    //          BucketTool BucketT = new BucketTool();
    //          BucketT.bucket(bucketPath, user, pass, dbset, dbname, qr);
    //        }
    
           if(fail ==0){
             BucketTool BucketT = new BucketTool();
             Thread t = new Thread(BucketT);
             t.start();
           }
    My question is, how do I pass my arguments into my Thread t? Seems like everything I try gives me a syntax error.

    Thanks.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default Re: Help with "implements Runnable"

    It looks like you would pass it into a constructor for BucketTool.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Brad is offline Member
    Join Date
    Feb 2012
    Posts
    21
    Rep Power
    0

    Default Re: Help with "implements Runnable"

    That worked! Thanks.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Help with "implements Runnable"

    If your GUI is a Swing GUI then it has the SwingWorker whose job it is to launch background tasks like this.

    That is assuming this is a task that is launched by the user interacting with the GUI.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 09:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 04:06 PM
  3. "extends" and "implements"
    By newbie101 in forum New To Java
    Replies: 6
    Last Post: 02-27-2012, 03:12 PM
  4. "implements from Runnable"
    By newbie101 in forum New To Java
    Replies: 1
    Last Post: 08-04-2011, 07:18 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

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