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

Discussion in 'Java' started by Bob Dubery, Jun 19, 2004.

  1. Bob Dubery

    Bob Dubery Guest

    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
    Bob Dubery, Jun 19, 2004
    #1
    1. Advertising

  2. Bob Dubery

    Woebegone Guest


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

    --
    HTH,
    Sean.


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.707 / Virus Database: 463 - Release Date: 16/06/04
    Woebegone, Jun 19, 2004
    #2
    1. Advertising

  3. Bob Dubery

    Bob Dubery Guest

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

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

    sorted out that ClassCastException problem.



    --
    Use our news server 'news.foorum.com' from anywhere.
    More details at: http://nnrpinfo.go.foorum.com/
    Bob Dubery, Jun 19, 2004
    #3
  4. Bob Dubery

    Jim Sculley Guest

    Bob Dubery wrote:
    > 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.
    --
    Remove my extraneous mandibular appendages to reply via email.
    Jim Sculley, Jun 19, 2004
    #4
  5. Bob Dubery

    Bob Dubery Guest

    Jim Sculley <> wrote in message news:<>...
    > Bob Dubery wrote:
    > > 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.

    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.
    Bob Dubery, Jun 19, 2004
    #5
  6. Bob Dubery

    Bob Dubery Guest

    "Woebegone" <> wrote in message news:<5kXAc.10944$>...
    > > 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.
    Bob Dubery, Jun 19, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mgalaty
    Replies:
    0
    Views:
    2,712
    mgalaty
    Sep 10, 2004
  2. patrick
    Replies:
    0
    Views:
    3,407
    patrick
    Aug 20, 2005
  3. gbattine
    Replies:
    5
    Views:
    5,519
    gbattine
    Jun 19, 2006
  4. gbattine
    Replies:
    8
    Views:
    5,769
    gbattine
    Jun 22, 2006
  5. ghashami_mina
    Replies:
    0
    Views:
    501
    ghashami_mina
    Dec 24, 2007
Loading...

Share This Page