Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

Thread problem (urgent)

Thread view: 
roy.sebastien@gmail.com - 15 Dec 2005 19:38 GMT
Hello All,

I have a class SpecificTask defined as follow:

public final class SpecificTask extends AbstractTask implements
Runnable {
 public void initialize() throws Exception {
   super.initialize();
   ...
 }
 public void execute() {
   System.out.println(isShutdownSignalReceived());
   Thread thread = new Thread(this, this.getClass().getName());
   try {
       thread.start();
   } catch (IllegalStateException ise) {
       logger.error("Couldn't start Recommendation Task thread");
   }
 }

 public synchronized void run() {
   while (!isShutdownSignalReceived()){
      doSomething();
   }
 }
}

and of course another class AbstractTask

public abstract class AbstractTask {
 private ServiceManager serviceManager = null;
 public void initialize() {
   shutdownSignalListener = (ShutdownSignalListener)
serviceManager.lookup(
           ShutdownSignalListener.ROLE);
 }
 protected boolean isShutdownSignalReceived() {
       return shutdownSignalListener.isShutdownSignalReceived();
 }
 ...
}

if I call the method isShutdownSignalReceived in the execute method of
the class SpecificTask, everything is fine.  Though, as soon as I call
it from the run() method, I get a nullpointerassignment.  If I debug
it, as soon as I get in the method isShutdownSignalReceived() from the
AbstractTask class, I clearly see that the field is null.  I have done
this in the past, what am I doing wrong this time?
Chris Smith - 15 Dec 2005 19:59 GMT
> if I call the method isShutdownSignalReceived in the execute method of
> the class SpecificTask, everything is fine.  Though, as soon as I call
> it from the run() method, I get a nullpointerassignment.  If I debug
> it, as soon as I get in the method isShutdownSignalReceived() from the
> AbstractTask class, I clearly see that the field is null.  I have done
> this in the past, what am I doing wrong this time?

There's nothing in the code you posted that would obviously cause your
problem.  However, it's equally obvious (if nothing else, from the
ellipses and the "doSomething") that the code you posted isn't the code
that's giving the error message.  If you can't be bothered to show us
the code that's failing, then it's rather difficult to help you.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Monique Y. Mudama - 15 Dec 2005 20:12 GMT
>> if I call the method isShutdownSignalReceived in the execute method
>> of the class SpecificTask, everything is fine.  Though, as soon as
[quoted text clipped - 10 lines]
> to show us the code that's failing, then it's rather difficult to
> help you.

I can't be bothered to read code when people try to tell me their
problem is "urgent."  I don't like the feeling that a complete
stranger expects me to drop everything I'm doing to help them right
this instant.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

VisionSet - 15 Dec 2005 21:02 GMT
> > If you can't be bothered
> > to show us the code that's failing, then it's rather difficult to
[quoted text clipped - 4 lines]
> stranger expects me to drop everything I'm doing to help them right
> this instant.

I rather like the phrase I heard the other day, probably on here:

"Lack of planning on your part does not constitute an emergency on mine."

LOL

Mike W
Monique Y. Mudama - 15 Dec 2005 21:14 GMT
>> > If you can't be bothered to show us the code that's failing, then
>> > it's rather difficult to help you.
[quoted text clipped - 8 lines]
> "Lack of planning on your part does not constitute an emergency on
> mine."

That was a post of mine, actually, quoting my father.

He says it thusly:

"Lack of planning on your part does not constitute an emergency on my
part."

Maybe the double use of "part" helps the flow.  Maybe not.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Chris Uppal - 16 Dec 2005 08:28 GMT
> He says it thusly:
>
> "Lack of planning on your part does not constitute an emergency on my
> part."
>
> Maybe the double use of "part" helps the flow.  Maybe not.

If you stop right after "emergency", then the expression gains even more force
as a dry put-down, I think.

(I don't have an opinion on whether a put-down is warranted in this case.)

   -- chris
Roedy Green - 16 Dec 2005 09:34 GMT
On Fri, 16 Dec 2005 08:28:32 -0000, "Chris Uppal"
<chris.uppal@metagnostic.REMOVE-THIS.org> wrote, quoted or indirectly
quoted someone who said :

>> "Lack of planning on your part does not constitute an emergency on my
>> part."
[quoted text clipped - 5 lines]
>
>(I don't have an opinion on whether a put-down is warranted in this case.)

But it is not true. If the twit has 5 assignments due tomorrow he well
may flunk out and his father will disown him, and he will spend the
rest of his life selling hotdogs.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Chris Uppal - 16 Dec 2005 10:32 GMT
> But it is not true. If the twit has 5 assignments due tomorrow he well
> may flunk out and his father will disown him, and he will spend the
> rest of his life selling hotdogs.

And the emergency is ?

(I'm still not taking any position on the OP's problems, but /if/ I had
thought it appropriate to say "lack of planning on your part does not
constitute an emergency", then I would certainly respond as above to an
explanation of the terrible consequences.)

   -- chris
Monique Y. Mudama - 16 Dec 2005 16:41 GMT
>> But it is not true. If the twit has 5 assignments due tomorrow he
>> well may flunk out and his father will disown him, and he will
[quoted text clipped - 8 lines]
>
>     -- chris

[disclaimer text=political]

If the culprit happens to be FEMA, I guess the lack of planning
really *does* constitute an emergency.

[/disclaimer]

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Chris Uppal - 17 Dec 2005 10:24 GMT
> If the culprit happens to be FEMA, I guess the lack of planning
> really *does* constitute an emergency.

Federation of European Motorcyclists' Associations ?  I admit that I might
hesitate before applying a "dry put-down" to even one biker, let alone a
federation of associations thereof, but the pictures of the committee at:
   http://www.fema.kaalium.com/howwework.htm
don't seem /too/ intimidating ;-)

   -- chris
Monique Y. Mudama - 19 Dec 2005 20:12 GMT
>> If the culprit happens to be FEMA, I guess the lack of planning
>> really *does* constitute an emergency.
[quoted text clipped - 4 lines]
> the committee at: http://www.fema.kaalium.com/howwework.htm don't
> seem /too/ intimidating ;-)

I actually meant the US Federal Emergency Management Agency, and was
referring to Hurricane Katrina.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Monique Y. Mudama - 16 Dec 2005 16:39 GMT
>> He says it thusly:
>>
[quoted text clipped - 8 lines]
> (I don't have an opinion on whether a put-down is warranted in this
> case.)

I think my dad used this a lot as a supervisor in the civil service.
There were probably limits to what he could get away with =)

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

ricky.clarkson@gmail.com - 16 Dec 2005 04:28 GMT
The problem is that you have forgotten to call initialize() before
starting the thread.

There are a few things you can do here to prevent this kind of bug:

1.  Convert initialize to a constructor, or at least make initialize
private and call it from a constructor.  It's impossible to construct
an object while forgetting to call a constructor, but it isn't
impossible to forget to call initialize(), obviously.  Probably you
think initialize() is a good idea because of applets, but the applet
API is very poor (it relies on subclassing, for one).

2.  Make all fields that you need to set ONCE, final.  This will make
sure that you set them, and only set them once.  This will probably
force you to implement suggestion 1.  Work with the compiler on this
one, don't fight the error messages.  This is probably the most
significant way of preventing NullPointerExceptions.

3.  Don't declare that you throw Exception.  Declare SPECIFIC types of
exception.  If you declare that you throw Exception, this forces the
caller to either catch or throw Exception, which will include
RuntimeException, masking real bugs.  Of course, the caller could use
instanceof to make sure that any RuntimeExceptions are rethrown, e.g:

try
{
  yourMethod();
}
catch (final Exception exception)
{
  if (exception instanceof RuntimeException)
     throw (RuntimeException)exception;
}

Even so, it's better to be explicit about the individual types of
exception thrown, so that the caller can choose to, e.g., handle a
FileNotFoundException differently to an SQLException.

4.  This is more of a design problem than an implementation problem,
but you seem to be programming to implementations, rather than
interfaces.  That is, there is no interface for Task, just an
AbstractTask.  You are coupling the implementation of AbstractTask with
the subtypes of it, which is entirely pointless.  It saves a few
keypresses, that's all, but loses a lot of flexibility.

5.  Don't use the 'null' keyword unless you mean it.  Setting a field
to null is unnecessary, it's null by default.  Also, if you use
suggestion 2, final, then Java will count the =null as The One
assignment, and signal all the other assignments as errors.  It's
better to omit it.
Chris Smith - 16 Dec 2005 15:35 GMT
> The problem is that you have forgotten to call initialize() before
> starting the thread.

That doesn't seem to be the case.  If that were true, then the call to
isShutdownSignalReceived() from the execute() method would have failed
with a NullPointerException.  That isn't what happened, according to
Roy.  Instead, Roy said that call succeeded, but then a later call to
the same method failed.

The only explanation is that Roy is mistaken about what's happening (and
therefore needs to provide more information), or that something in code
that he did not post is causing the problem (in which case he needs to
provide more information).

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

ricky.clarkson@gmail.com - 17 Dec 2005 16:45 GMT
Chris Smith,

>> The problem is that you have forgotten to call initialize() before
>> starting the thread.

>That doesn't seem to be the case.  If that were true, then the call to
>isShutdownSignalReceived() from the execute() method would have failed
>with a NullPointerException.  That isn't what happened, according to
>Roy.  Instead, Roy said that call succeeded, but then a later call to
>the same method failed.

I should have been more clear.  He has (at least) two instances, one of
which he calls initialize() on, one of which he does not.

Ricky.
Chris Smith - 17 Dec 2005 17:47 GMT
> I should have been more clear.  He has (at least) two instances, one of
> which he calls initialize() on, one of which he does not.

Okay, so Roy: do you ever call the run() method or create a new Thread
to run an instance of SpecificTask other than by calling execute()?

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

ricky.clarkson@gmail.com - 17 Dec 2005 18:06 GMT
Chris,

For cases like this, I'm fond of Debugging By Refactoring, so if he
follows by advice about making a constructor containing the stuff that
initialize() contains, the compiler will point out the bug.  If it
doesn't then there is some inaccuracy in either his description or my
interpretation.

Yes, I just made up 'Debugging By Refactoring'.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.