Thread problem (urgent)

R

roy.sebastien

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?
 
C

Chris Smith

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.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Monique Y. Mudama

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.

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.
 
V

VisionSet

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.

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
 
M

Monique Y. Mudama

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."

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.
 
R

ricky.clarkson

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.
 
C

Chris Uppal

Monique said:
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
 
R

Roedy Green

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.)

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.
 
C

Chris Uppal

Roedy said:
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
 
C

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.

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).

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Monique Y. Mudama

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.)

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 =)
 
M

Monique Y. Mudama

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

[disclaimer text=political]

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

[/disclaimer]
 
C

Chris Uppal

Monique said:
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
 
R

ricky.clarkson

Chris Smith,
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.
 
C

Chris Smith

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()?

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

ricky.clarkson

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'.
 
M

Monique Y. Mudama

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 ;-)

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top