Skip to content

Commit ef8d5dc

Browse files
pacamaraPedro Pombeiro
authored andcommitted
Use thread pool instead of new threads on Android. Closes status-im#3690
Signed-off-by: Pedro Pombeiro <pombeirp@users.noreply.github.com>
1 parent a521cc7 commit ef8d5dc

4 files changed

Lines changed: 65 additions & 26 deletions

File tree

‎android/app/src/main/java/im/status/ethereum/MainActivity.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.instabug.library.Instabug;
2828

2929
import java.util.Properties;
30+
import im.status.ethereum.module.StatusThreadPoolExecutor;
3031

3132
public class MainActivity extends ReactActivity
3233
implements ActivityCompat.OnRequestPermissionsResultCallback{
@@ -40,6 +41,7 @@ private static void registerUncaughtExceptionHandler(final Context context) {
4041
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
4142
@Override
4243
public void uncaughtException(final Thread thread, final Throwable t) {
44+
// High priority, so don't use StatusThreadPoolExecutor
4345
new Thread() {
4446
@Override
4547
public void run() {
@@ -150,14 +152,14 @@ public void onCancel(DialogInterface dialog) {
150152
dialog.show();
151153
}
152154

153-
Thread thread = new Thread() {
155+
Runnable r = new Runnable() {
154156
@Override
155157
public void run() {
156158
System.loadLibrary("status-logs");
157159
}
158160
};
159161

160-
thread.start();
162+
StatusThreadPoolExecutor.getInstance().execute(r);
161163
}
162164

163165
@Override

‎modules/react-native-status/android/src/main/java/im/status/ethereum/module/JSCJail.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ public String web3send(String payload) {
102102

103103
JSFunction web3sendAsync = new JSFunction(context, "web3sendAsync") {
104104
public void web3sendAsync(final String payload, final JSValue callback) {
105-
Thread thread = new Thread() {
105+
Runnable r = new Runnable() {
106106
@Override
107107
public void run() {
108108
String result = Statusgo.CallRPC(payload);
109109
callback.toFunction().call(null, result);
110110
}
111111
};
112112

113-
thread.start();
113+
StatusThreadPoolExecutor.getInstance().execute(r);
114114
}
115115
};
116116
context.property("web3sendAsync", web3sendAsync);

‎modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -356,28 +356,28 @@ public void startNode(final String config) {
356356
return;
357357
}
358358

359-
Thread thread = new Thread() {
359+
Runnable r = new Runnable() {
360360
@Override
361361
public void run() {
362362
doStartNode(config);
363363
}
364364
};
365365

366-
thread.start();
366+
StatusThreadPoolExecutor.getInstance().execute(r);
367367
}
368368

369369
@ReactMethod
370370
public void stopNode() {
371371

372-
Thread thread = new Thread() {
372+
Runnable r = new Runnable() {
373373
@Override
374374
public void run() {
375375
Log.d(TAG, "stopNode");
376376
String res = Statusgo.StopNode();
377377
}
378378
};
379379

380-
thread.start();
380+
StatusThreadPoolExecutor.getInstance().execute(r);
381381
}
382382

383383
@ReactMethod
@@ -390,7 +390,7 @@ public void login(final String address, final String password, final Callback ca
390390

391391
jail.reset();
392392

393-
Thread thread = new Thread() {
393+
Runnable r = new Runnable() {
394394
@Override
395395
public void run() {
396396
String result = Statusgo.Login(address, password);
@@ -399,7 +399,7 @@ public void run() {
399399
}
400400
};
401401

402-
thread.start();
402+
StatusThreadPoolExecutor.getInstance().execute(r);
403403
}
404404

405405
@ReactMethod
@@ -410,7 +410,7 @@ public void createAccount(final String password, final Callback callback) {
410410
return;
411411
}
412412

413-
Thread thread = new Thread() {
413+
Runnable r = new Runnable() {
414414
@Override
415415
public void run() {
416416
String res = Statusgo.CreateAccount(password);
@@ -419,7 +419,7 @@ public void run() {
419419
}
420420
};
421421

422-
thread.start();
422+
StatusThreadPoolExecutor.getInstance().execute(r);
423423
}
424424

425425
@ReactMethod
@@ -430,7 +430,7 @@ public void notifyUsers(final String message, final String payloadJSON, final St
430430
return;
431431
}
432432

433-
Thread thread = new Thread() {
433+
Runnable r = new Runnable() {
434434
@Override
435435
public void run() {
436436
String res = Statusgo.NotifyUsers(message, payloadJSON, tokensJSON);
@@ -439,7 +439,7 @@ public void run() {
439439
}
440440
};
441441

442-
thread.start();
442+
StatusThreadPoolExecutor.getInstance().execute(r);
443443
}
444444

445445
@ReactMethod
@@ -450,7 +450,7 @@ public void addPeer(final String enode, final Callback callback) {
450450
return;
451451
}
452452

453-
Thread thread = new Thread() {
453+
Runnable r = new Runnable() {
454454
@Override
455455
public void run() {
456456
String res = Statusgo.AddPeer(enode);
@@ -459,7 +459,7 @@ public void run() {
459459
}
460460
};
461461

462-
thread.start();
462+
StatusThreadPoolExecutor.getInstance().execute(r);
463463
}
464464

465465

@@ -470,7 +470,7 @@ public void recoverAccount(final String passphrase, final String password, final
470470
callback.invoke(false);
471471
return;
472472
}
473-
Thread thread = new Thread() {
473+
Runnable r = new Runnable() {
474474
@Override
475475
public void run() {
476476
String res = Statusgo.RecoverAccount(password, passphrase);
@@ -479,7 +479,7 @@ public void run() {
479479
}
480480
};
481481

482-
thread.start();
482+
StatusThreadPoolExecutor.getInstance().execute(r);
483483
}
484484

485485
private String createIdentifier() {
@@ -494,15 +494,15 @@ public void completeTransactions(final String hashes, final String password, fin
494494
return;
495495
}
496496

497-
Thread thread = new Thread() {
497+
Runnable r = new Runnable() {
498498
@Override
499499
public void run() {
500500
String res = Statusgo.CompleteTransactions(hashes, password);
501501
callback.invoke(res);
502502
}
503503
};
504504

505-
thread.start();
505+
StatusThreadPoolExecutor.getInstance().execute(r);
506506
}
507507

508508

@@ -513,14 +513,14 @@ public void discardTransaction(final String id) {
513513
return;
514514
}
515515

516-
Thread thread = new Thread() {
516+
Runnable r = new Runnable() {
517517
@Override
518518
public void run() {
519519
Statusgo.DiscardTransaction(id);
520520
}
521521
};
522522

523-
thread.start();
523+
StatusThreadPoolExecutor.getInstance().execute(r);
524524
}
525525

526526
// Jail
@@ -533,7 +533,7 @@ public void initJail(final String js, final Callback callback) {
533533
return;
534534
}
535535

536-
Thread thread = new Thread() {
536+
Runnable r = new Runnable() {
537537
@Override
538538
public void run() {
539539
jail.initJail(js);
@@ -542,7 +542,7 @@ public void run() {
542542
}
543543
};
544544

545-
thread.start();
545+
StatusThreadPoolExecutor.getInstance().execute(r);
546546
}
547547

548548
@ReactMethod
@@ -684,15 +684,15 @@ public void onConnectorDisconnected() {
684684

685685
@ReactMethod
686686
public void sendWeb3Request(final String payload, final Callback callback) {
687-
Thread thread = new Thread() {
687+
Runnable r = new Runnable() {
688688
@Override
689689
public void run() {
690690
String res = Statusgo.CallRPC(payload);
691691
callback.invoke(res);
692692
}
693693
};
694694

695-
thread.start();
695+
StatusThreadPoolExecutor.getInstance().execute(r);
696696
}
697697

698698
@ReactMethod
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package im.status.ethereum.module;
2+
3+
import java.util.concurrent.*;
4+
5+
public class StatusThreadPoolExecutor {
6+
private static final int NUMBER_OF_CORES =
7+
Runtime.getRuntime().availableProcessors();
8+
private static final int KEEP_ALIVE_TIME = 1;
9+
private static final TimeUnit KEEP_ALIVE_TIME_UNIT = TimeUnit.SECONDS;
10+
11+
private final BlockingQueue<Runnable> mQueue;
12+
private final ThreadPoolExecutor mThreadPool;
13+
14+
private StatusThreadPoolExecutor() {
15+
mQueue = new LinkedBlockingQueue<>();
16+
17+
mThreadPool = new ThreadPoolExecutor(
18+
NUMBER_OF_CORES,
19+
NUMBER_OF_CORES,
20+
KEEP_ALIVE_TIME,
21+
KEEP_ALIVE_TIME_UNIT,
22+
mQueue);
23+
}
24+
25+
/** Pugh singleton */
26+
private static class Holder {
27+
private static StatusThreadPoolExecutor instance = new StatusThreadPoolExecutor();
28+
}
29+
30+
public static StatusThreadPoolExecutor getInstance() {
31+
return Holder.instance;
32+
}
33+
34+
public void execute(final Runnable r) {
35+
mThreadPool.execute(r);
36+
}
37+
}

0 commit comments

Comments
 (0)