Results 1 to 13 of 13
- 06-18-2011, 03:18 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
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:
I initialize it using :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) { } } }
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 :).Java Code:new PropertiesFile().run();
Thanks!
Martin
- 06-18-2011, 03:39 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-18-2011, 03:47 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
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?
-
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.
- 06-18-2011, 03:53 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
You mean something like :
Runnable runnable = new PropertiesFile();
Thread thread = new Thread(runnable);
thread.start();
?
- 06-18-2011, 03:54 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
Check the online documentation of executor before using it, my code may be slightly off.Java Code:Thread t = new Thread(new PropertiesFile()); t.start(); Or Executor exec = Executor.newSingleThreadPool(); exec.submit(new PropertiesFile());
- 06-18-2011, 03:56 PM #7
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
- 06-18-2011, 04:06 PM #8
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
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!
- 06-18-2011, 04:12 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
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:
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.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
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-18-2011, 04:19 PM #10
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
- 06-18-2011, 04:25 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-18-2011, 06:01 PM #12
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
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!
- 06-18-2011, 06:16 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Generic question on implementing bluetooth/net code
By SM2010 in forum New To JavaReplies: 2Last Post: 12-14-2010, 04:10 PM -
Private inner & Runnable class instantiation failure
By jvmakela in forum Advanced JavaReplies: 1Last Post: 10-30-2010, 03:44 PM -
Need Urgent Help in Implementing Interface Class
By yel_hiei in forum New To JavaReplies: 4Last Post: 07-29-2010, 01:42 PM -
need help with implementing class
By ALH813 in forum New To JavaReplies: 3Last Post: 10-01-2009, 11:04 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks