Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(205)

Side by Side Diff: webrtc/examples/androidapp/src/org/appspot/apprtc/util/LooperExecutor.java

Issue 2206473003: Remove LooperExecutor and replace it with built-in HandlerThread. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Changes according to magjed's comments. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 package org.appspot.apprtc.util;
12
13 import android.os.Handler;
14 import android.os.Looper;
15 import android.util.Log;
16
17 import java.util.LinkedList;
18 import java.util.List;
19 import java.util.concurrent.Executor;
20
21 /**
22 * Looper based executor class. This is needed because WebSocketClient from auto banh requires the
23 * thread to have a looper. The class is used in WebSocketRTCClient/WebSocketCha nnelClient.
24 */
25 public class LooperExecutor extends Thread implements Executor {
26 private static final String TAG = "LooperExecutor";
27 // Object used to signal that looper thread has started and Handler instance
28 // associated with looper thread has been allocated.
29 private final Object looperStartedEvent = new Object();
30 private final List<Runnable> scheduledPeriodicRunnables = new LinkedList<Runna ble>();
31 private Handler handler = null;
32 private boolean running = false;
33 private long threadId;
34
35 @Override
36 public void run() {
37 Looper.prepare();
38 synchronized (looperStartedEvent) {
39 Log.d(TAG, "Looper thread started.");
40 handler = new Handler();
41 threadId = Thread.currentThread().getId();
42 looperStartedEvent.notify();
43 }
44 Looper.loop();
45 }
46
47 public synchronized void requestStart() {
48 if (running) {
49 return;
50 }
51 running = true;
52 handler = null;
53 start();
54 // Wait for Hander allocation.
55 synchronized (looperStartedEvent) {
56 while (handler == null) {
57 try {
58 looperStartedEvent.wait();
59 } catch (InterruptedException e) {
60 Log.e(TAG, "Can not start looper thread");
61 running = false;
62 }
63 }
64 }
65 }
66
67 public synchronized void requestStop() {
68 if (!running) {
69 return;
70 }
71 running = false;
72 handler.post(new Runnable() {
73 @Override
74 public void run() {
75 handler.getLooper().quit();
76 Log.d(TAG, "Looper thread finished.");
77 }
78 });
79 }
80
81 // Checks if current thread is a looper thread.
82 public boolean checkOnLooperThread() {
83 return (Thread.currentThread().getId() == threadId);
84 }
85
86 public synchronized void scheduleAtFixedRate(final Runnable command, final lon g periodMillis) {
87 if (!running) {
88 Log.w(TAG, "Trying to schedule task for non running executor");
89 return;
90 }
91 Runnable runnable = new Runnable() {
92 @Override
93 public void run() {
94 if (running) {
95 command.run();
96 if (!handler.postDelayed(this, periodMillis)) {
97 Log.e(TAG, "Failed to post a delayed runnable in the chain.");
98 }
99 }
100 }
101 };
102 scheduledPeriodicRunnables.add(runnable);
103 if (!handler.postDelayed(runnable, periodMillis)) {
104 Log.e(TAG, "Failed to post a delayed runnable.");
105 }
106 }
107
108 public synchronized void cancelScheduledTasks() {
109 if (!running) {
110 Log.w(TAG, "Trying to cancel schedule tasks for non running executor");
111 return;
112 }
113
114 // Stop scheduled periodic tasks.
115 for (Runnable r : scheduledPeriodicRunnables) {
116 handler.removeCallbacks(r);
117 }
118 scheduledPeriodicRunnables.clear();
119 }
120
121 @Override
122 public synchronized void execute(final Runnable runnable) {
123 if (!running) {
124 Log.w(TAG, "Running looper executor without calling requestStart()");
125 return;
126 }
127 if (Thread.currentThread().getId() == threadId) {
128 runnable.run();
129 } else {
130 handler.post(runnable);
131 }
132 }
133
134 /**
135 * Access to the handler for testing purposes.
136 */
137 Handler getHandler() {
138 return handler;
139 }
140 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698