Chromium Code Reviews| Index: components/cronet/android/java/src/org/chromium/net/urlconnection/MessageLoop.java |
| diff --git a/components/cronet/android/java/src/org/chromium/net/urlconnection/MessageLoop.java b/components/cronet/android/java/src/org/chromium/net/urlconnection/MessageLoop.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1a967cdc33384d3667516856337f25f8f3876256 |
| --- /dev/null |
| +++ b/components/cronet/android/java/src/org/chromium/net/urlconnection/MessageLoop.java |
| @@ -0,0 +1,77 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.net.urlconnection; |
| + |
| +import java.util.concurrent.BlockingQueue; |
| +import java.util.concurrent.LinkedBlockingQueue; |
| + |
| +/** |
| + * A package protected MessageLoop class for use in |
| + * {@link CronetHttpURLConnection} |
| + */ |
| +final class MessageLoop { |
| + private final BlockingQueue<Runnable> mQueue; |
| + // A reusable runnable to quit the message loop. |
| + private final Runnable mQuitTask; |
| + // Indicates whether this message loop is currently running. |
| + private boolean mLoopRunning; |
| + |
| + public MessageLoop() { |
| + mQueue = new LinkedBlockingQueue<Runnable>(); |
| + mQuitTask = new Runnable() { |
| + @Override |
| + public void run() { |
| + mLoopRunning = false; |
| + } |
| + }; |
| + } |
| + |
| + /** |
| + * Runs the message loop. Be sure to call {@link MessageLoop#postQuitTask()} |
| + * to end the loop. |
| + */ |
| + public void loop() { |
| + if (mLoopRunning) { |
| + throw new IllegalStateException( |
| + "Cannot run loop when it is already running."); |
| + } |
| + mLoopRunning = true; |
| + while (mLoopRunning) { |
| + try { |
| + Runnable task = mQueue.take(); // Blocks if the queue is empty. |
| + task.run(); |
| + } catch (InterruptedException e) { |
| + throw new RuntimeException(e); |
|
xunjieli
2014/11/26 16:11:56
I am not sure what I should do to handle Interrupt
mef
2014/11/26 16:36:04
It sounds like you can get that exception when you
mmenke
2014/11/26 17:09:12
Yes, it should quit the loop (Which re-throwing th
xunjieli
2014/11/26 18:37:14
Done.
|
| + } |
| + } |
| + } |
| + |
| + /** |
| + * Posts a reusable runnable, {@link #mQuitTask} to quit the loop. This |
| + * causes the {@link #loop()} to terminate after processing all currently |
| + * enqueued messages. |
| + */ |
| + public void postQuitTask() { |
| + try { |
| + mQueue.put(mQuitTask); |
| + } catch (InterruptedException e) { |
| + throw new RuntimeException(e); |
| + } |
| + } |
| + |
| + /** |
| + * Posts a task to the message loop. |
| + */ |
| + public void postTask(Runnable task) { |
| + if (task == null) { |
| + throw new IllegalArgumentException(); |
| + } |
| + try { |
| + mQueue.put(task); |
| + } catch (InterruptedException e) { |
| + throw new RuntimeException(e); |
| + } |
| + } |
| +} |