Java

Java

By DeepSource

Expensive methods should not be invoked from performance critical threads JAVA-P1007

Performance

Performance critical threads shouldn't be blocked by expensive jobs.

Methods annotated with @MainThread, @UIThread, or @PerformanceCritical generally execute on threads that shouldn't be blocked by expensive jobs. Doing so might affect the overall responsiveness of the application.

@Expensive and @WorkerThread are used to annotate methods that are expensive to execute. Hence such methods must not be invoked from methods marked as @MainThread, @UIThread, or @PerformanceCritical.

Examples

Bad Practice

@WorkerThread // or @Expensive
void doExpensiveJob() {
 // ..expensive task goes here
}

@MainThread // or @UIThread, or @PerformanceCritical
public void someMethod() {
    doExpensiveJob();
}

Recommended

Consider executing the task in a worker thread.

@MainThread // or @UIThread, or @PerformanceCritical
public void someMethod() {
    workerThreadPool.submitTask(new Runnable() {
        public void run() {
            doExpensiveJob();
        }
    });
}

References