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