views:

511

answers:

7

I am just curious on how people solve this. I often write the same type of code all the time. For instance:

new Thread() {
   //...
   //...
   //...
   //Change this line
   //...
   //...
}.start();

I keep changing the line where it says "Change this line" and then starting a thread. This change can be one line or a few lines. How would I go about compacting this code?

+7  A: 

One technique is to put the code in an anonymous inner class, and pass that to a method that does the rest.

interface SomeInterface {
    void fn();
}

    executeTask(new SomeInterface {
        public void fn() {
            // Change this line.
        }
    });

private void executeTask(final SomeInterface thing) {
     Thread thread = new Thread(new Runnable() { public void run() {
        //...
        //...
        //...
        thing.fn();
        //...
        //...
     }});
     thread.start();
}

Generally it isn't a good idea to extend Thread or other classes if it is unnecessary.

Tom Hawtin - tackline
A: 

Nope, no macros. For this case, the closest you can get is to create new Runnable instance and pass it to either a function of your own creation or an ExecutorService, which will start the task for you.

With Java, there's no good way to get rid of this "boilerplate" code, mainly because you can't pass pointers to functions; everything needs to be an object.

Outlaw Programmer
+1  A: 

Well, I guess you could run your java files through the C preprocessor...

gnud
That's not the craziest thing I've ever heard, but it's darn near close!
Outlaw Programmer
Agreed - it's not a pretty picture.
gnud
+1  A: 

It's possible to use Java annotations to generate boilerplate code. Writing your own annotation processor is not hard.

djna
A: 

In the case of a Thread, you can pass any object that implements Runnable to its constructor.

So, the solution is to create your own class:

public class MyClass implements Runnable {
    void run() {
        // change this line
    }
}

Unfortunately, run isn't static, so you'll have to create an instance of MyClass first:

new Thread(new MyClass()).start();

You could also add variables to MyClass and a constructor so you can pass arguments to it.

Edit: If you need more than just the start method, you can also subclass Thread itself.

R. Bemrose
+3  A: 

You can use the template pattern to create a base class that contains the common code. For example:

public abstract class ThreadTemplate extends Thread
{

    public void run() {
        //reusable stuff
        doInThread();
        //more resusable stuff
    }

    abstract void doInThread();

}

Then starting a thread with the boilerplate code is as easy as:

new ThreadTemplate{
   void doInThread() {
       // do something
   }
}.start();

Also, a less elegant solution to save yourself some typing is to use the templating feature of your ide. You can find some info on setting them up in Eclipse here and you can find a list of useful ones at http://stackoverflow.com/questions/1028858/useful-eclipse-java-code-templates

Jason Gritman
Not sure why you're getting modded down, this is the _exact_ right answer if, from what he says, those //... lines are common.
Nick Veys
I had a spelling error, maybe that was it?
Jason Gritman
+1  A: 

If it is something that you use in many projects, I would set this up in your IDE. For instance I use eclipse and if you go to Window->Preferences->Java->Editor->Templates you can setup your own templates, and then have them auto complete. For instance I always start typing sysout and then press tab, and eclipse has a built in template to replace that with System.out.println();

There are many pre-built templates in eclipse, so you could look at those for examples on syntax, if that is the IDE you use, if not there may be something similiar in other IDE's

This is very useful, and you could create one for any boiler code you find yourself writing a lot.

broschb