java - Android AsyncTask progress update hang -


so had crack @ coding own asynctask class system runs on threadpoolexecutor natively. working fine until decided implement progress side of things. progress works asynctask, onprogressupdate function called on ui thread. problem i'm experiencing whenever there system.out or log.x line in onprogressupdate hangs indefinitely no error or warning oddly. code below:

 public abstract class task<a, b> {      private static final executor executor = getexecutor();     private static final int default_priority = thread.min_priority;     private static final int default_progress_increment = 1;      private static final executor getexecutor() {         threadpoolexecutor executor = (threadpoolexecutor) executors.newcachedthreadpool();         executor.setcorepoolsize(1);         executor.allowcorethreadtimeout(false);         // todo set rejection handler         //executor.setrejectedexecutionhandler(new handler());         // todo set thread factory         executor.prestartcorethread();         return executor;     }      public static class executionlistener<b> {          public void onpreexecute() {             log.i("task", "pre - thread: " + thread.currentthread().getid());         }          public void onpostexecute(b output) {              log.i("task", "post - thread: " + thread.currentthread().getid() + " - output: " + output);         }          public void onprogressupdate(int progress) {             log.d("task", "hello");         }     }      private handler handler;     private executionlistener<b> executionlistener;     private volatile int progress = 0;     private atomicboolean progresspublished = new atomicboolean(true);     private b output;      public task() {         this.handler = new handler();         this.executionlistener = new executionlistener();     }      public void setexecutionlistener(executionlistener executionlistener) {         if(executionlistener == null) {             this.executionlistener = new executionlistener();         }         else {             this.executionlistener = executionlistener;         }     }      protected void updateprogress(int progressmade) {         log.d("task", "test");         progress += progressmade;         if(progresspublished.compareandset(true, false)) {             if(!handler.post(new runnable() {                 @override                 public void run() {                     log.d("task", new integer(progress).tostring() + " - a");                     executionlistener.onprogressupdate(progress);                     // hangs below                     progresspublished.lazyset(true);                     log.d("task", new integer(progress).tostring() + " - b");                 }             })) {                 log.d("task", "failed post");             }         }     }      protected void updateprogress() {         updateprogress(default_progress_increment);     }      protected abstract b dotask(a input);      public void execute(final input, final int priority) {         executor.execute(new runnable() {             @override             public void run() {                 thread.currentthread().setpriority(priority);                 handler.post(new runnable() {                     @override                     public void run() {                         executionlistener.onpreexecute();                     }                 });                 output = dotask(input);                 if(!handler.post(new runnable() {                     @override                     public void run() {                         log.d("task", "done");                         executionlistener.onpostexecute(output);                     }                 })) {                     log.d("task", "failed post post");                 }             }         });     }      public void execute(final input) {         execute(input, default_priority);     } } 

the executionlistener class override methods run on ui asynctask's methods doing same. code uses runnable objects execute dotask method , send updates / result appropriate method in executionlistener.

the thread.currentthread() parts ensure things running on thread intended them to. problem shows when running task calls updateprogress() - have tried putting thread sleep in onprogressupdate() method , seems solve things, though isn't solution.

it seems have problem log.x / system.out - don't know whether call frequency of either of them cause kind of issue. i'm @ loss progress feature , logging advice appreciated - i've found quite hard explain please ask if need me clarify anything!

turns out thread.currentthread().getid() @ fault. removing part fixes everything. follow question here: is java thread getid() thread-safe?


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -