Java: Is there support for macros?

后端 未结 7 1192
暗喜
暗喜 2021-02-12 17:47

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         


        
相关标签:
7条回答
  • 2021-02-12 18:23

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

    0 讨论(0)
  • 2021-02-12 18:27

    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.

    0 讨论(0)
  • 2021-02-12 18:27

    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.

    0 讨论(0)
  • 2021-02-12 18:28

    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.

    0 讨论(0)
  • 2021-02-12 18:28

    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 Useful Eclipse Java Code Templates

    0 讨论(0)
  • 2021-02-12 18:45

    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.

    0 讨论(0)
提交回复
热议问题