Java, pass main class to child class, bad coding style?
Please bear with me on this as I'm still getting the hang on Java.
The example bellow reads from a Class named Parent
that creates an instance of itself on the main
method. It then makes that instance do all sorts of calculations.
Next it fires up a thread named Child
passing the Parent
instance as a reference to Child
.
The Child
just sits there monitoring things and, sometimes, fires up public methods on Parent
.
It works. The question is, is this poor style? Is there a more Java thinking way of doing this sort of work?
public class Parent {
// main function that fires up the program
public static void main() {
// creates an instance of himself
// and fires go that does all sorts of fuzzy calculus
Parent parent = new Parent();
parent.go();
// creates a new thread of child and starts it
Child child = new Child(parent);
child.start();
}
private void go() {
// all sort of initializations
}
public void getDataFromChild(int data) {
// will get data from running child thread
}
}
public class Child extends Thread {
private Parent parent;
// child constructor grabs Parent instance into "o"
public Child(Parent o) {
p开发者_C百科arent = o;
}
// this class main loop
public void run() {
while(1==1) {
doSomething();
try {
sleep(1000);
}
catch(Exception e) { }
}
}
private void doSomething() {
parent.getDataFromChild(1);
}
}
Thank you.
Subclassing Thread is considered bad style. It's better to implement Runnable, and then hand the runnable off to a thread (hmmm... quite analogous to your Parent/Child hand-off!).
Runnable r = new Child(parent);
new Thread(r).start();
Otherwise your Child.java code looks fine to me.
To implement Runnable you just need to provide a run() method:
public class Child implements Runnable {
private Parent parent;
public Child(Parent parent) { this.parent = parent; }
public void run() {
// what the thread does goes in here...
}
The question is, is this poor style? Is there a more Java thinking way of doing this sort of work?
Passing a "child" object a reference to its "parent" is neither good style or bad style. It is just programming.
In a real application (rather than your highly artificial example) you could make a judgement as to whether or not this was good design.
In addition to Julius Davies' answer, I have another concern about code like this:
Child child = new Child(parent);
Namely, why is the child in its own thread, and what is the child going to do with the parent?
If the parent really is as trivial as it appears to be, then the child thread doesn't seem to be necessary. On the other hand, if the parent does more work after the child thread is started, then you have synchronization issues that you need to deal with. And you must deal with them. Murphy's Law says that any threading problems that can happen, will happen. (Corallary: There are more possible threading issues than you're aware of, even if you're an expert on Murphy's Law.)
EDIT: OK, the child thread calls parent.getDataFromChild
. What does that method do with the data? (Does it make any changes to objects that are also accessed from the main thread?) Since it's called from the child thread, you need to inspect both the parent and child classes for possible race conditions.
I don't see anything wrong with this except the Child
class should implement Runnable
. As long as you document the relation between Parent
and Child
it looks fine.
You'll want to override the finalize method on the parent to clean up Child if they're referenced elsewhere. Otherwise it seems perfectly acceptable to me.
Regarding what you wrote it is better for Child to be Runnable that runs in a Thread
http://www.javabeginner.com/learn-java/java-threads-tutorial
then regarding the question of whether it is a good way the answer is : it depends what you want to do LOL
You can look into several things:
- Catching various exceptions like
InterruptedException
reading up a synchronized to prevent simultaneous access/modifications.
instead of sleep you can check the observer class/design pattern to control when your threads wake to do there tests
Finally I would like to great you to the wonderful world of Threading your app in Java :D
Edit: as Dave L pointed out as well you need to pay attention to clean up what you reference otherwise you'll end up with zombies in you program ^^
What I read from your code is a cycle dependency which is bad.
You may make use of interfaces to remove the dependency.
精彩评论