The shortest way to run a simple background task?

I've seen at least five templates through which you can run some code in a workflow. Simplest:

new Thread(new Runnable() {
    public void run() {
      //
    }
}).start();

      

We can continue AsyncTask

; we have AsyncTaskLoader

others Loader

for data, Service

etc.

I am assuming that they each have some advantages that I am not talking about here. I'm wondering: what is the most suitable and concise way to start a simple action?

Simple definition:

  • Fairly short action, <1s;
  • No need to deal with UI elements;
  • One line or so, so it is impractical to extend with a AsyncTask

    new class;
  • You do not need to notify about the success of the task in Activity

    or Fragment

    ;
  • No need to display progress;
  • You just need to have some return

    .

At first I thought:

boolean myReturn;

new Thread(new Runnable() {
    public void run() {
      myReturn = myExtensiveTask();
    }
}).start();

public boolean myExtensiveTask() { ... }

      

Will it be correct (or even possible)? How to do it?

+3


source to share


2 answers


Using a bolts

service infrastructure (used by Facebook and parse.com), it's simple:



Task.callInBackground(new Callable<MyReturnType>() {
    @Override
    public MyReturnType call() {
        MyReturnType obj = doWork();
        return obj;
    }
});

      

+2


source


The best solution will probably be related to some library code.

I have implemented something in my current project that allows me to write a method like this:

@QSchedule("0 * * * * ?")
public void runsEveryMinute()
{
    System.out.println("It ran!");
}

      

We use them all over the place. The scheduler runs on top of QuartzScheduler. I suppose spring allows syntax a lot like this, just by importing Quartz support.

If you're creative, many solutions can work this way. For example, if you have a class that created a menu, you can abstract away all of the menu building syntax and guide it using annotations. I did that too. The structure could be something like this:

@MenuItem("File","Save")
public void fileSave() {
    // This is executed when file save is selected
}
@MenuItem("File","Load")
public void fileLoad() {
    // Executed when file load is seleclted
}

      

These annotations provide all the information you need to create the entire menu system, so another class just looks at your annotated class and creates a menu for it. Somewhere you have code that looks like this:



 Menu mainMenu = new MenuBuilder().buildMenu(MenuDefinition.class);

      

Where MenuDefinition.class contains annotations as above. The best part about this solution? 100% reusable - just throw it into any project and you never have to manually create the menu again (or a button or any other redundant task bothers you).

I am not saying that this will solve your problem directly, and I am saying that you can code a little infrastructure to make your future calls easier. Annotations are a pretty cool way of specifying what code you want to run as Java has such an ugly anonymous inner class.

Oh, also Java 8 should do what you want to be trivial. In Groovy (which has closures similar to Java 8), you can use something very close to this:

new Thread() {
    println "this will run in another thread"
}

      

(I can't guarantee the exact syntax, but it's really close)

0


source







All Articles