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

Side by Side Diff: components/cronet/android/java/src/org/chromium/net/urlconnection/MessageLoop.java

Issue 725683002: [Cronet] Initial implementation of HttpURLConnection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Exception handling Created 6 years 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 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.net.urlconnection;
6
7 import java.util.concurrent.BlockingQueue;
8 import java.util.concurrent.LinkedBlockingQueue;
9
10 /**
11 * A package protected MessageLoop class for use in
12 * {@link CronetHttpURLConnection}
13 */
14 final class MessageLoop {
15 private final BlockingQueue<Runnable> mQueue;
16 // A reusable runnable to quit the message loop.
17 private final Runnable mQuitTask;
18 // Indicates whether this message loop is currently running.
19 private boolean mLoopRunning;
20
21 public MessageLoop() {
22 mQueue = new LinkedBlockingQueue<Runnable>();
23 mQuitTask = new Runnable() {
24 @Override
25 public void run() {
26 mLoopRunning = false;
27 }
28 };
29 }
30
31 /**
32 * Runs the message loop. Be sure to call {@link MessageLoop#postQuitTask()}
33 * to end the loop.
34 * @throws InterruptedException
35 */
36 public void loop() throws InterruptedException {
37 if (mLoopRunning) {
38 throw new IllegalStateException(
39 "Cannot run loop when it is already running.");
40 }
41 mLoopRunning = true;
42 while (mLoopRunning) {
43 Runnable task = mQueue.take(); // Blocks if the queue is empty.
44 task.run();
45 }
46 }
47
48 /**
49 * Posts a reusable runnable, {@link #mQuitTask} to quit the loop. This
50 * causes the {@link #loop()} to terminate after processing all currently
51 * enqueued messages.
52 * @throws InterruptedException
53 */
54 public void postQuitTask() throws InterruptedException {
55 mQueue.put(mQuitTask);
56 }
57
58 /**
59 * Posts a task to the message loop.
60 * @throws InterruptedException
61 */
62 public void postTask(Runnable task) throws InterruptedException {
63 if (task == null) {
64 throw new IllegalArgumentException();
65 }
66 mQueue.put(task);
67 }
68 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698