Results 1 to 13 of 13
  1. #1
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default Question about a class implementing Runnable

    Hello everyone

    I made an application using a custom PropertiesFile class which i made implement Runnable. Here it is in its entirety:

    Java Code:
    package Utils;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Properties;
    import java.util.logging.Level;
    
    public class PropertiesFile implements Runnable
    {
        private static Properties props;
        
        @Override
        public void run()
        {
            try
            {
                props = new Properties();
                FileInputStream fis = new FileInputStream("config/config.ini");
                props.load(fis);
                fis.close();
                Logs.getPropsLog().log(Level.INFO, "Successfully loaded properties file");
            }
            catch(IOException ex)
            {
                Logs.getPropsLog().log(Level.SEVERE, "Unable to load properties file : " + ex.getMessage(), ex);
            }
        }
    
        public static String getProperty(String name)
        {
    
            String propValue = props.getProperty(name);
            Logs.getPropsLog().log(Level.INFO, "Reading " + name + " property and returning " + propValue);
            return propValue;
        }
    
        public static void setProperty(String name, String value)
        {
            props.setProperty(name, value);
            try
            {
                props.store(new FileOutputStream("config/config.ini"), null);
            }
            catch (Exception ex)
            {
                
            }
        }
    }
    I initialize it using :

    Java Code:
    new PropertiesFile().run();
    My question is this : Will this class continuously run in its own thread? When i call getProperty(), does it stay in its thread? If not, how would i accomplish this. You might ask why do this with a simple config access class but i'm also doing it with my database handler, hibernate util, data access objects, etc and that was the shortest one :).

    Thanks!

    Martin

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,437
    Blog Entries
    7
    Rep Power
    20

    Default

    What other Thread? I don't see another Thread? A Runnable hasn't much to do with Threads except for the fact that a Thread can run a Runnable when you construct one with it. Your class runs in one single thread. b.t.w. why do you initialize your object in the run() method instead of in its constructor?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    See i was told that for a class to run in its own thread you can either extend Thread or implement Runnable (BS?). I thought whatever was in the run method would run in its own thread. In fact, i have made an animation class which has a while true loop in the run method and it works fine since the GUI is still responsive, therefore it HAS to be running in its own thread, unless i have this totally wrong. The reason i use run() is because there is no need for a constructor in this class as i dont pass anything to it. So i guess my question now becomes : how do i make this class run in its own thread at all times?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by d3n1s View Post
    See i was told that for a class to run in its own thread you can either extend Thread or implement Runnable (BS?).
    You usually have the class implement Runnable and put the background code in the run method, yes, but for it to run as a thread, the object must be placed in a Thread constructor and start called on the thread. The threading tutorials cover this all, and I suggest you read one or two before moving forward.

  5. #5
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    You mean something like :

    Runnable runnable = new PropertiesFile();
    Thread thread = new Thread(runnable);
    thread.start();

    ?

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You create a runnable and than pass it to an executor service, or a thread. Calling the run method how you did is no different than just callig any method. You could pass it to a thread(or executor) like this
    Java Code:
    Thread t = new Thread(new PropertiesFile());
    t.start();  
    
    Or
    
    Executor exec = Executor.newSingleThreadPool();
    exec.submit(new PropertiesFile());
    Check the online documentation of executor before using it, my code may be slightly off.

  7. #7
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    You create a runnable and than pass it to an executor service, or a thread. Calling the run method how you did is no different than just callig any method. You could pass it to a thread(or executor) like this
    Java Code:
    Thread t = new Thread(new PropertiesFile());
    t.start();  
    
    Or
    
    Executor exec = Executor.newSingleThreadPool();
    exec.submit(new PropertiesFile());
    Check the online documentation of executor before using it, my code may be slightly off.
    Wow that was synchronized!

  8. #8
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    Seems to work fine. I had to add Thread.sleep(1000) at a few places to give my database time to initialize properly but it works fine. Since i am using miniserver (you should seriously give that a check if you need an extremly portable mySQL db BTW) it needs a few seconds to create the mapped drives and start the service.

    Thanks for the incredibly quick response guys!

    Cheers!

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,437
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by d3n1s View Post
    The reason i use run() is because there is no need for a constructor in this class as i dont pass anything to it. So i guess my question now becomes : how do i make this class run in its own thread at all times?
    And exactly that can be extremely dangerous; you are probably working alone on this program so nothing will explode in your face but: if an object is constructed at a certain moment in time and the object needs to be further constructed/initialized in a next step (e.g. your run() method) later in time there exists a time span where your object is totally unusable; in your case:

    Java Code:
    PropertiesFile pf= new PropertiesFile(); // not fully operational yet
    pf.setProperty("foo", "bar"); // this explodes in your face
    pf.run(); // too late, the program crashed already
    This 'two phase construction' is just sloppy coding, don't do it; make your constructors deliver complete objects or don't deliver anything at all by throwing an Exception. Your constructor should do a complete job.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    And exactly that can be extremely dangerous; you are probably working alone on this program so nothing will explode in your face but: if an object is constructed at a certain moment in time and the object needs to be further constructed/initialized in a next step (e.g. your run() method) later in time there exists a time span where your object is totally unusable; in your case:

    Java Code:
    PropertiesFile pf= new PropertiesFile(); // not fully operational yet
    pf.setProperty("foo", "bar"); // this explodes in your face
    pf.run(); // too late, the program crashed already
    This 'two phase construction' is just sloppy coding, don't do it; make your constructors deliver complete objects or don't deliver anything at all by throwing an Exception. Your constructor should do a complete job.

    kind regards,

    Jos
    That makes total sense. So i should put the Thread inside the class and call it in the Object constructor so this way it will be ready to rock when the constructor finishes running am i getting this right?

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,437
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by d3n1s View Post
    That makes total sense. So i should put the Thread inside the class and call it in the Object constructor so this way it will be ready to rock when the constructor finishes running am i getting this right?
    Yep, you're correct; only hand out complete, workable, operational objects; what the rest of your program wants to do with it then is completely their business. The only situation where this 'two phase construction' is fine is when it is constructed by a factory so the object isn't known to the rest of the world until the factory is ready with the object. And even then, you should be aware of what you're doing.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    Well i just discovered the join() method which waits for a thread to die before continuing execution. By using that in conjuction with the nested thread i was able to get rid of the Thread.sleep() calls i had to insert once in a while and also get rid of the user-configured sleep times. It will now sleep by itself as long as needed even if the machine is a p3 with 64mb of ram.

    Peace!

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,437
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by d3n1s View Post
    even if the machine is a p3 with 64mb of ram.
    A few decades ago that was considered a huge, fast machine, youg man. e.g. an IBM 370/158 had 256K words of memory and served hundeds of people working on it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 2
    Last Post: 12-14-2010, 04:10 PM
  2. Private inner & Runnable class instantiation failure
    By jvmakela in forum Advanced Java
    Replies: 1
    Last Post: 10-30-2010, 03:44 PM
  3. Need Urgent Help in Implementing Interface Class
    By yel_hiei in forum New To Java
    Replies: 4
    Last Post: 07-29-2010, 01:42 PM
  4. need help with implementing class
    By ALH813 in forum New To Java
    Replies: 3
    Last Post: 10-01-2009, 11:04 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
  •