Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Advanced-java] enclosing class creating a thread advice


  • From: nikolaos@xxxxxxxxx (Nikolaos Giannopoulos)
  • Subject: [Advanced-java] enclosing class creating a thread advice
  • Date: Thu, 14 Mar 2002 13:53:32 -0000

> From: advanced-java-admin@xxxxxxxxxxxxxxxxxxxxxx
> [mailto:advanced-java-admin@xxxxxxxxxxxxxxxxxxxxxx]On Behalf Of Tim
> O'Neil
>
> I have a class that spins off a thread to do some work on a
> data file. This class hangs subordinate classes out to do
> i/o and random access file work that must be referenced by
> the thread. I am currently passing a reference to the entire
> enclosing class to the thread so it has all the stuff it needs
> to do its thing. This works, but it seems kinda ugly to me.
> I wonder if this is the way to go or does this seem like a
> scenario for reflection or some other method?


If you are passing in a specific class then this probably seems ugly because
the solution doesn't seem flexible enough.  How about doing the following:

public interface ThreadWork {
  public void doWork();
}

public class SomeThreadWorkA implements ThreadWork {
  public SomeThreadWorkA() {}
  public void doWork() {
    // Actual Work specific to this class A
  }
}

Now your ThreadWorker can hold references to ThreadWork objects and simply
invoke doWork() on without really caring what the actual work does - since
it is specific to the work itself (and encapsulated therein) and not part of
the interface.

HTH.

--Nikolaos