• Post Reply Bookmark Topic Watch Topic
  • New Topic

Code that looks like a first class function  RSS feed

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So...
I'm working with a book that is giving me some advice on how to use a Runnable to get a piece of action working in another thread. The example is from swing but the question I have is not specific to swing. The example goes like this.

I just don't understand this syntax. I know Java doesn't have first class functions but that's what this looks like to me. It also kinda looks like using an adapter but runnable is an interface not a class that implements one one so the "public void run()" isn't overriding anything it can only be implementing a required method right? What is this type of instantiation all about plx?
[ June 22, 2008: Message edited by: Tristan Rouse ]
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the syntax for an anonymous class. In general, it looks like...

The result is an instance of a class with no name (and therefore anonymous), whose reference is automatically upcast to the type following "new." So in your example, this is an anonymous class that implements Runnable.

For more detail, see Anonymous inner classes from Eckel's Thinking in Java.
[ June 22, 2008: Message edited by: marc weber ]
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that folks have claimed that this is, in fact, Java's answer to first-class functions, or to function pointers (depending on your language background.)
 
Leroy J Brown
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can definitely see why people would suggest that. I have one more question on the topic if you don't mind....

In using this type of anonymous class for the reason originally stated does everything inside the doWorkStuff() method being called run inside the EDT? If so does any method the doWorkStuff() method calls also run in this thread... and so on?

this would be of course by using the
javax.swing.SwingUtilities.invokeLater(toDoInEventHandlerThread);
method if I understand this right...
and I'm not so sure on that
[ June 23, 2008: Message edited by: Tristan Rouse ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a method calls another method, then always, without exception, the called method runs synchronously in the same thread as the caller. There's no magic syntax in Java to call a method any other way. There is only the magic library class Thread, and it's method start(), which arranges to call run() in a new thread. start() can't be written in Java -- it must be written in native code, as part of the JVM implementation.

So yes, if run() is called from the Swing worker thread, and it calls doWorkStuff(), then doWorkStuff() runs on that worker thread. And any methods doWorkStuff() calls run there too.

Does that answer your question?
 
Leroy J Brown
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and then some.
thanks friend.
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unlike SwingUtils.invokeAndWait, SwingUtils.invokeLater (or better yet, EventQueue.invokeLater) works when called from the EDT. The code will be run sometime after the current method has finished. It's purpose is to be called from different threads though.

you should also check out javax.swing.SwingWorker. It can do a lot of the nasty threading stuff for you; all you need to do is override a few methods.
[ June 24, 2008: Message edited by: Rob Prime ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!