2010/9/30 sebb <seb...@gmail.com>:
> On 30 September 2010 02:58, Niall Pemberton <niall.pember...@gmail.com> wrote:
>> On Thu, Sep 30, 2010 at 2:46 AM, sebb <seb...@gmail.com> wrote:
>>> Just wondering if the Tailer API could be simplified by performing the
>>> thread start within the class?
>>> Is it ever going to be useful to have direct access to tailer thread?
>>> I suspect not, as the Listener should provide sufficient access.
>>>
>>> It's not safe to start a thread in the constructor (unless the ctor is
>>> final), but one could use static factory methods instead.
>>>
>>> So instead of:
>>>
>>>  TailerListener listener = ...
>>>  Tailer tailer = new Tailer(file, listener, delay);
>>>  Thread thread = new Thread(tailer);
>>>  thread.start();
>>>  ...
>>>  tailer.stop()
>>>
>>> one would do something like:
>>>
>>>  TailerListener listener = ...
>>>  Tailer tailer = Tailer.createTailer(file, listener, delay);
>>>  ...
>>>  tailer.stop()
>>>
>>> This simplifies the API, and allows the class to force the thread to
>>> be a daemon thread. It also stops the caller from messing with the
>>> thread.
>>>
>>> WDYT?
>>
>> There are five different Thread constructors that take a Runnable and
>> a bunch of other methods that someone might want to use. I don't have
>> a problem providing a convenience static method - but it only saves
>> two lines of code - as long as they can construct one manually with or
>> without a Thread if they want to.
>
> But what is the use case for having access to the created thread?
> Seems to me it would be a lot safer if the thread were private to the class.

The use case is that someone may want to use an Executor instead of a
dedicated Thread to run background tasks such as Tailer, at least I
do. I have no problem with providing a convenient static method, as
Niall suggested, but I would like to keep the Tailer as a Runnable
with no embedded thread if possible.

HTH,
Julien

>
> It would still be possible to use the code without a thread by
> exposing the method that loops over the file, and using the
> constructor instead.
> For example with the current code one could do something like:
>
> TailerListener listener = ...
> Tailer tailer = new Tailer(file, listener, delay);
> tailer.run()
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
> For additional commands, e-mail: dev-h...@commons.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
For additional commands, e-mail: dev-h...@commons.apache.org

Reply via email to