java - ScheduledExecuterService.scheduleAtFixedRate creating multiple thread pools - Android -


i have asynctask called uploadmanager in android application, checks processed items, , uploads them server.
purpose i'm using scheduledexecutorservice.scheduleatfixedrate check items after every 1 minute, , upload them server.
however, @ times, there multiple pools of thread created (this happens 10% of times), due @ times same request send twice server, though concurrency handled @ both server & client level, still don't want happen on client side.
below how code looks like.
in mainactivity (start-activity), initiate uploadmanager as:

public class mainactivity extends baseactivity {     @override    public void oncreate(bundle savedinstancestate) {        super.oncreate(savedinstancestate);        .        .        .        new uploadmanager().execute(this);    } } 

uploadmanager works follows:

public class uploadmanager extends asynctask<context, integer, integer> {      private scheduledexecutorservice scheduledexecutorservice;     private static final int num_of_threads = 5;     private static final int delay_in_seconds = 60;     private context context;     private final logger logger = new logger(getclass().getsimplename());      protected integer doinbackground(context... context) {         this.context = context[0];         scheduledexecutorservice = executors.newscheduledthreadpool(num_of_threads);         scheduledexecutorservice.scheduleatfixedrate(postinformation, 5, delay_in_seconds, timeunit.seconds);        return 0;     }      private runnable postinformation = new runnable() {         @override         public void run() {             if (networkmanager.isinternetavailable(context)) {                 uploadacknowledgement();             }         }     };      private void uploadacknowledgement() {         list<acknowledgement> acklist = null;         try {             logger.info("running task post acknowledgement");             .             .         }     } } 

when checked logs, says:

35119 [pool-2-thread-1] info  upload manager - [1363841355530] : 21/03/2013 10:19:15 : running task post acknowledgement   35122 [pool-3-thread-1] info  upload manager - [1363841355532] : 21/03/2013 10:19:15 : running task post acknowledgement 

which indicates there multiple pools of thread now, due which, uploadacknowledgement() method called multiple-times.

the mainactivity declared in androidmanifest.xml as:

<application android:label="@string/app_name" android:icon="@drawable/icon">     <activity android:name="mainactivity"               android:label="@string/app_name"               android:theme="@android:style/theme.notitlebar">         <intent-filter>             <action android:name="android.intent.action.main"/>             <category android:name="android.intent.category.launcher"/>             <action android:name="android.net.conn.connectivity_change"/>         </intent-filter>     </activity>     .     . 

i figured out reasons, if scheduledexecutor's service runs overlap, spawns new worker threads (eg. in case postinformation() creates new worker thread) , eventually, new pool.
however, multiple workers can avoided using newsinglethreadscheduledexecutor instead of newscheduledthreadpool(int corepoolsize)'.
so, instead of initialising `scheduledexecutorservice' as:

scheduledexecutorservice = executors.newscheduledthreadpool(num_of_threads);   

it should have been initialised as:

scheduledexecutorservice = executors.newsinglethreadscheduledexecutor();   

this way ensured there no multiple worker threads running.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -