Chromium Code Reviews| OLD | NEW |
|---|---|
| (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.io.IOException; | |
| 8 import java.util.concurrent.BlockingQueue; | |
| 9 import java.util.concurrent.LinkedBlockingQueue; | |
| 10 | |
| 11 /** | |
| 12 * A package protected MessageLoop class for use in | |
| 13 * {@link CronetHttpURLConnection}. | |
|
mmenke
2014/12/03 16:21:01
Should mention the mLoopTerminated behavior either
xunjieli
2014/12/03 19:03:30
Done.
| |
| 14 */ | |
| 15 final class MessageLoop { | |
|
mmenke
2014/12/03 16:21:01
Can we just make this implement Executor instead,
| |
| 16 private final BlockingQueue<Runnable> mQueue; | |
| 17 // A reusable runnable to quit the message loop. | |
| 18 private final Runnable mQuitTask; | |
| 19 // Indicates whether this message loop is currently running. | |
| 20 private boolean mLoopRunning; | |
| 21 // Indicates whether the loop has been terminated. It cannot be started if | |
| 22 // it has been terminated. | |
| 23 private boolean mLoopTerminated; | |
|
mmenke
2014/12/03 16:21:01
Suggest explicitly setting these to false - relyin
mmenke
2014/12/03 16:21:01
Maybe "It cannot be started..." -> "It cannot be s
mmenke
2014/12/03 16:21:01
I don't think "terminated" is a great term here, s
xunjieli
2014/12/03 19:03:30
Done.
xunjieli
2014/12/03 19:03:30
Done. I named it mWaitingInterrupted, as it is not
xunjieli
2014/12/03 19:03:30
Done.
| |
| 24 | |
| 25 public MessageLoop() { | |
| 26 mQueue = new LinkedBlockingQueue<Runnable>(); | |
| 27 mQuitTask = new Runnable() { | |
| 28 @Override | |
| 29 public void run() { | |
| 30 mLoopRunning = false; | |
| 31 } | |
| 32 }; | |
| 33 } | |
| 34 | |
| 35 /** | |
| 36 * Runs the message loop. Be sure to call {@link MessageLoop#postQuitTask()} | |
| 37 * to end the loop. | |
| 38 * @throws IOException | |
| 39 */ | |
| 40 public void loop() throws IOException { | |
| 41 if (mLoopTerminated) { | |
| 42 throw new IllegalStateException( | |
| 43 "Cannot run loop as the loop has been terminated."); | |
| 44 } | |
| 45 if (mLoopRunning) { | |
| 46 throw new IllegalStateException( | |
| 47 "Cannot run loop when it is already running."); | |
| 48 } | |
| 49 mLoopRunning = true; | |
| 50 while (mLoopRunning) { | |
| 51 try { | |
| 52 Runnable task = mQueue.take(); // Blocks if the queue is empty. | |
| 53 task.run(); | |
| 54 } catch (InterruptedException e) { | |
|
mmenke
2014/12/03 16:21:01
Can we test this?
xunjieli
2014/12/03 19:03:30
Done.
| |
| 55 mLoopRunning = false; | |
| 56 mLoopTerminated = true; | |
| 57 throw new IOException(e); | |
| 58 } | |
|
mmenke
2014/12/03 16:21:01
Out of paranoia, maybe catch any RunTimeException,
xunjieli
2014/12/03 19:03:30
But InterruptedException is not a runtime exceptio
| |
| 59 } | |
| 60 } | |
| 61 | |
| 62 /** | |
| 63 * Posts a reusable runnable, {@link #mQuitTask} to quit the loop. This | |
| 64 * causes the {@link #loop()} to terminate after processing all currently | |
| 65 * enqueued messages. | |
| 66 * @throws IOException | |
| 67 */ | |
| 68 public void postQuitTask() throws IOException { | |
| 69 try { | |
| 70 mQueue.put(mQuitTask); | |
| 71 } catch (InterruptedException e) { | |
| 72 throw new IOException(e); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 /** | |
| 77 * Posts a task to the message loop. | |
| 78 * @throws IOException | |
| 79 */ | |
| 80 public void postTask(Runnable task) throws IOException { | |
| 81 if (task == null) { | |
| 82 throw new IllegalArgumentException(); | |
| 83 } | |
| 84 try { | |
| 85 mQueue.put(task); | |
| 86 } catch (InterruptedException e) { | |
| 87 throw new IOException(e); | |
| 88 } | |
| 89 } | |
| 90 } | |
| OLD | NEW |