Skip to content

Commit

Permalink
[BKNDLSS-23755,BKNDLSS-23637,BKNDLSS-23406] thread pool service (#482)
Browse files Browse the repository at this point in the history
* Refactoring of ThreadPoolService and ThreadFactoryService.

* Fixes in build.gradle

* Removed Subscription.
  • Loading branch information
oleg-vyalyh authored Jan 18, 2021
1 parent feb8aae commit 92712f6
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 352 deletions.
2 changes: 1 addition & 1 deletion src/com/backendless/Backendless.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public static void initApp( Object context, final String applicationId, final St

if( isCodeRunner() )
{
ThreadPoolService.getPoolExecutor();
ThreadPoolService.getThreadPoolExecutor();
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/com/backendless/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public <T> void get( final String key, final AsyncCallback<T> callback )
{
final Class<?> asyncCallbackType = ReflectionUtil.getCallbackGenericType( callback );

ThreadPoolService.getPoolExecutor().execute( new Runnable()
ThreadPoolService.getThreadPoolExecutor().execute(new Runnable()
{
@Override
public void run()
Expand Down
2 changes: 1 addition & 1 deletion src/com/backendless/Invoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static <T> void invokeAsync( String className, String methodName, Object[
public static <T> void invokeAsync( final String className, final String methodName, final Object[] args,
final AsyncCallback<T> callback, final IChainedResponder responder )
{
ThreadPoolService.getPoolExecutor().execute( new Runnable()
ThreadPoolService.getThreadPoolExecutor().execute(new Runnable()
{
@Override
public void run()
Expand Down
127 changes: 0 additions & 127 deletions src/com/backendless/Subscription.java

This file was deleted.

53 changes: 0 additions & 53 deletions src/com/backendless/ThreadFactoryService.java

This file was deleted.

44 changes: 30 additions & 14 deletions src/com/backendless/ThreadPoolService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@

package com.backendless;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
Expand All @@ -29,30 +28,48 @@

public class ThreadPoolService
{
private final static ExecutorService threadPoolExecutor;
private final static SimpleThreadFactory THREAD_FACTORY;
private final static ThreadPoolExecutor THREAD_POOL_EXECUTOR;
private final static ScheduledThreadPoolExecutor SCHEDULED_THREAD_POOL_EXECUTOR;
private final static int MAX_THREAD_POOL_SIZE = 10;

static
{
if (Backendless.isCodeRunner())
threadPoolExecutor = new ThreadPoolExecutor(0, MAX_THREAD_POOL_SIZE, 2, TimeUnit.SECONDS,
{
THREAD_FACTORY = new SimpleThreadFactory("BackendlessSDK_CR");
THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(0, MAX_THREAD_POOL_SIZE, 2, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(),
new SimpleThreadFactory("BackendlessSDK_CR"));
THREAD_FACTORY);

SCHEDULED_THREAD_POOL_EXECUTOR = null;
}
else
threadPoolExecutor = new ThreadPoolExecutor(2, MAX_THREAD_POOL_SIZE, 60, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(),
new SimpleThreadFactory("BackendlessSDK"));
{
THREAD_FACTORY = new SimpleThreadFactory("BackendlessSDK");
THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(2, MAX_THREAD_POOL_SIZE, 60, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(),
THREAD_FACTORY);

SCHEDULED_THREAD_POOL_EXECUTOR = new ScheduledThreadPoolExecutor(2, THREAD_FACTORY);
}
}

public static ExecutorService getPoolExecutor()
public static ThreadPoolExecutor getThreadPoolExecutor()
{
return threadPoolExecutor;
return THREAD_POOL_EXECUTOR;
}

public static ScheduledThreadPoolExecutor getScheduledThreadPoolExecutor()
{
return SCHEDULED_THREAD_POOL_EXECUTOR;
}


private static class SimpleThreadFactory implements ThreadFactory
{
private final ThreadFactory threadFactory = Executors.defaultThreadFactory();
private final static ThreadGroup THREAD_GROUP = new ThreadGroup( "BackendlessSDK_ThreadGroup" );

private final String threadNamePrefix;
private final boolean isDaemon = true;
private final AtomicInteger threadNumber = new AtomicInteger();
Expand All @@ -63,10 +80,9 @@ public SimpleThreadFactory( String poolName )
}

@Override
public Thread newThread( Runnable r )
public Thread newThread( Runnable runnable )
{
Thread t = threadFactory.newThread( r );
t.setName( threadNamePrefix + threadNumber.getAndIncrement() );
Thread t = new Thread( THREAD_GROUP, runnable, threadNamePrefix + threadNumber.getAndIncrement() );
t.setDaemon( isDaemon );
return t;
}
Expand Down
2 changes: 1 addition & 1 deletion src/com/backendless/UploadBitmapAsyncTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void executeThis( Bitmap bitmap, Bitmap.CompressFormat compressFormat, int quali
private void doInBackground( final Bitmap bitmap, final Bitmap.CompressFormat compressFormat, final int quality,
final String name, final String path )
{
ThreadPoolService.getPoolExecutor().execute( new Runnable()
ThreadPoolService.getThreadPoolExecutor().execute(new Runnable()
{
@Override
public void run()
Expand Down
2 changes: 1 addition & 1 deletion src/com/backendless/UploadFileAsyncTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private void doInBackground( final File file, final String path )
{
final AsyncUploadMessage asyncUploadMessage = new AsyncUploadMessage( uploadCallback );

ThreadPoolService.getPoolExecutor().execute( new Runnable()
ThreadPoolService.getThreadPoolExecutor().execute(new Runnable()
{
@Override
public void run()
Expand Down
85 changes: 0 additions & 85 deletions src/com/backendless/messaging/AndroidHandler.java

This file was deleted.

Loading

0 comments on commit 92712f6

Please sign in to comment.