java.lang.ClassCastException when calling a method of a thread

B

Bob Dubery

Hi all... I'm having fun with threads again...

Here's the class that runs as a thread...
class Ass2Q3 extends Thread {
// this is the class that is invoked as a thread
private boolean stopFlag;
private boolean pauseFlag;
private int threadNum;
private String threadId;

// constuctor
public Ass2Q3(int n){
threadNum = n;
threadId = "Thread " + threadNum;
// thread is neither paused nor stopped at
// construction time
stopFlag = false;
pauseFlag = false;
}

public void run(){
while(! stopFlag){
int timeToSleep;
try{
if(pauseFlag){
timeToSleep = 50000;
}
else{
timeToSleep = 1000;
}
Thread.currentThread().sleep(timeToSleep);
}
catch(InterruptedException ie){
}
}
}

public void end(){
stopFlag = true;
}

public void pause(){
pauseFlag = true;
}

public void unPause(){
// take an active thread out of pause state
pauseFlag = false;
Thread.currentThread().interrupt();
}

public boolean isActive(){
boolean activeFlag = true;
if(pauseFlag){
activeFlag = false;
}
return activeFlag;
}
}

Now I have an array of Thread objects...
private Thread[] threadInstances = new Thread[10];

And I create a thread like THIS...
threadInstances[threadSelected] = new Thread(new
Ass2Q3(threadSelected));

And set it actually running like THIS...
threadInstances[threadSelected].start();

And all of that works fine. But when I want to call the pause method I
do this...
( (Ass2Q3) threadInstances[threadSelected]).pause();

And then I get a java.lang.ClassCastException

As I understand that, it means that I'm treating
threadInstances[threadSelected]). as an instance of a class that isn't
and isn't a subclass of.

If I want to call a method of an instance of Ass2Q3 running as a
seperate thread then how should I go about that?

Thanks

Bob
 
W

Woebegone

threadInstances[threadSelected] = new Thread(new
Ass2Q3(threadSelected));

This creates a Thread object that has an Ass2Q3 as its Runnable. I think you
want to say just "threadInstances[threadSelected] = new
Ass2Q3(threadSelected);" Ass2Q3 is-a Thread by extension, but the Thread
object you create isn't an Ass2Q3.
( (Ass2Q3) threadInstances[threadSelected]).pause();

This cast is unnecessary after the change above. Initially it's incorrect
because as decsribed above, "threadInstances[threadSelected]" is a Thread,
but it is *not* an Ass2Q3.
 
B

Bob Dubery

My apologies! About 10 minutes after posting this I realised my error.

Changing to
threadInstances[threadSelected] = new Ass2Q3(threadSelected);

sorted out that ClassCastException problem.
 
J

Jim Sculley

Bob said:
Hi all... I'm having fun with threads again...

Here's the class that runs as a thread...
class Ass2Q3 extends Thread {

Don't extend Thread. Implement Runnable instead.

<snip>

Your start(), stop(), pause(), etc methods aren't very threadsafe.
Now I have an array of Thread objects...
private Thread[] threadInstances = new Thread[10];

And I create a thread like THIS...
threadInstances[threadSelected] = new Thread(new
Ass2Q3(threadSelected));

Uhm, you extended Thread. You can just write the above as:

threadInstances[threadSelected] = new Ass2Q3(threadSelected);

That is actually the source of your problem. However, if you had
impemented Runnable, which is the preferred way to do things, the line
would look like your original.
And set it actually running like THIS...
threadInstances[threadSelected].start();

And all of that works fine. But when I want to call the pause method I
do this...
( (Ass2Q3) threadInstances[threadSelected]).pause();

And then I get a java.lang.ClassCastException

Yes. Because you array is populated with Thread objects, not Ass2Q3
objects. You wrote:

threadInstances[threadSelected] =
new Thread(new Ass2Q3(threadSelected));

This is like writing

threadInstances[threadSelected] = new Thread();

because you wrapped your Ass2Q3 object in yet another Thread object.

Jim S.
 
B

Bob Dubery

Jim Sculley said:
Don't extend Thread. Implement Runnable instead.

<snip>

Your start(), stop(), pause(), etc methods aren't very threadsafe.
No they're not. But for this exercise they don't need to be.

Not THAT good an excuse...

Thanks. I figured out what the error was about 10 minutes after this
posting. Now I can tidy the code up.

Thanks for the help.
 
B

Bob Dubery

Woebegone said:
threadInstances[threadSelected] = new Thread(new
Ass2Q3(threadSelected));

This creates a Thread object that has an Ass2Q3 as its Runnable. I think you
want to say just "threadInstances[threadSelected] = new
Ass2Q3(threadSelected);" Ass2Q3 is-a Thread by extension, but the Thread
object you create isn't an Ass2Q3.

Yes. Figured it out after posting - so maybe I should have gone one
more round with it before taking up other people's time.

Thanks for replying. It is appreciated.
 

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

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top