Exception

Im getting the following exception

java.lang.ClassCastException

at java.awt.Dialog$1.run(Unknown Source)

at java.awt.event.InvocationEvent.dispatch(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

But im not using Dialog class anywhere in my code!! Following is the code which is creating the problem!!

public void actionPerformed(ActionEvent ae) {

final int index = getColorSettings();

if(index>=0) {

try{

Thread thread = new Thread(Thread.currentThread());

thread.setPriority(Thread.MAX_PRIORITY);

thread.start();

System.out.println("Befor Entering Exit Thread");

SwingUtilities.invokeLater(new Runnable(){

public void run() {

bExit = pdcsinstance[index].closeRequest(); //Displays an exit confirmation page

System.out.println("Exception while calling closeRequest method");

}

});

}catch(Exception oe) {

System.out.println("Close Exception");

}

}

}

[1460 byte] By [B@ssia] at [2007-10-2 1:49:36]
# 1
Thread thread = new Thread(Thread.currentThread());thread.setPriority(Thread.MAX_PRIORITY);thread.start();Errrrrrrrr.....What are you trying to do there...............?
B@ssia at 2007-7-15 19:30:49 > top of Java-index,Java Essentials,Java Programming...
# 2

Ok, so heres my guess....

The current thread is the EventDispatchThread.

So, you are starting a new thread which does the same thing as the EventDispatchThread.

This new thread starts servicing AWT events.

Many of these type of events get back to the event dispatch thread using something like:

public void run() {

EventDispatchThread dispatcher = (EventDispatchThread)Thread.currientThread();

// ...

}

If this happens on your new dodgy thread, then there's going to be a ClassCastException.

If I am right, I am going to go to the pub this very instant and reward myself with a pint or two - as this is one of the most interesting posts I've ever seen :o)

(Hint - get rid of that dodgy thread you are creating)

B@ssia at 2007-7-15 19:30:49 > top of Java-index,Java Essentials,Java Programming...
# 3

> > Thread thread = new Thread(Thread.currentThread());

> thread.setPriority(Thread.MAX_PRIORITY);

> thread.start();

>

>

> Errrrrrrrr.....

>

> What are you trying to do there...............?

yeah, AFAIK, Thread.currentThread() is, by definition, started and running... I wonder what actually happens when thread.start() gets executed...

Torajiroua at 2007-7-15 19:30:49 > top of Java-index,Java Essentials,Java Programming...
# 4

> yeah, AFAIK, Thread.currentThread() is, by definition, started and running... I wonder what actually happens when thread.start() gets executed...

Well, as the nutters decided to make Thread implement runnable, the original thread has "run" invoked on the new thread of execution.

This in turn runs its target - so the original runnable gets run.

A security risk wouldn't you say?

Anyway, try it out with this:

public class T2 {

public static void main(String[] args) throws Exception {

Thread t = new Thread(new MyRunnable());

t.start();

}

public static class MyRunnable implements Runnable {

public void run() {

System.err.println("Hello from " + Thread.currentThread());

Thread t = new Thread(Thread.currentThread());

t.start();

System.err.println("Started " + t);

System.err.println("Waiting for completion");

try {

t.join();

} catch (InterruptedException e) {

throw new RuntimeException(e);

}

}

}

}

Torajiroua at 2007-7-15 19:30:49 > top of Java-index,Java Essentials,Java Programming...
# 5
@ B@ssi :Cheers for providing the most interesting post in a long time.~D
Torajiroua at 2007-7-15 19:30:49 > top of Java-index,Java Essentials,Java Programming...
# 6

And, as if by magic, in java.awt.Dialog:

Runnable pumpEventsForHierarchy = new Runnable() {

public void run() {

EventDispatchThread dispatchThread =

(EventDispatchThread)Thread.currentThread();

dispatchThread.pumpEventsForHierarchy(new Conditional() {

public boolean evaluate() {

return keepBlocking && windowClosingException == null;

}

}, Dialog.this);

}

};

Torajiroua at 2007-7-15 19:30:49 > top of Java-index,Java Essentials,Java Programming...
# 7
hey but my prob's not yet solved guys!!what should i do to get rid of this exception?i have quite a few jbuttons in my application, so how do i make sure that code writtem inside those action listeners is executed on the EventDispatchThread?Pls reply fast!!
B@ssia at 2007-7-15 19:30:49 > top of Java-index,Java Essentials,Java Programming...
# 8

> Pls reply fast!!

Its better not to put stuff like that - it reduces your chances of getting a reply as it gets peoples back up.

> what should i do to get rid of this exception?

Have you got rid of that crazy code that created a new thread yet?

If not, get rid of that and try again.

B@ssia at 2007-7-15 19:30:49 > top of Java-index,Java Essentials,Java Programming...
# 9

the problem is that if i remove these 3 lines:

Thread thread = new Thread(Thread.currentThread());

thread.setPriority(Thread.MAX_PRIORITY);

thread.start();

The closeRequest method is supposed to bring up a JDialog box, and if i comment the above 3 lines, that JDialog freezes..

B@ssia at 2007-7-15 19:30:49 > top of Java-index,Java Essentials,Java Programming...