• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.incallui.async;
18 
19 import java.util.concurrent.Executors;
20 
21 import javax.annotation.concurrent.ThreadSafe;
22 
23 /**
24  * {@link PausableExecutor} for use in tests. It is intended to be used between one test thread
25  * and one prod thread. See {@link com.android.incallui.ringtone.InCallTonePlayerTest} for example
26  * usage.
27  */
28 @ThreadSafe
29 public final class SingleProdThreadExecutor implements PausableExecutor {
30 
31     private int mMilestonesReached;
32     private int mMilestonesAcked;
33     private boolean mHasAckedAllMilestones;
34 
35     @Override
milestone()36     public synchronized void milestone() {
37         ++mMilestonesReached;
38         notify();
39         while (!mHasAckedAllMilestones && mMilestonesReached > mMilestonesAcked) {
40             try {
41                 wait();
42             } catch (InterruptedException e) {}
43         }
44     }
45 
46     @Override
ackMilestoneForTesting()47     public synchronized void ackMilestoneForTesting() {
48         ++mMilestonesAcked;
49         notify();
50     }
51 
52     @Override
ackAllMilestonesForTesting()53     public synchronized void ackAllMilestonesForTesting() {
54         mHasAckedAllMilestones = true;
55         notify();
56     }
57 
58     @Override
awaitMilestoneForTesting()59     public synchronized void awaitMilestoneForTesting() throws InterruptedException {
60         while (!mHasAckedAllMilestones && mMilestonesReached <= mMilestonesAcked) {
61             wait();
62         }
63     }
64 
65     @Override
execute(Runnable command)66     public synchronized void execute(Runnable command) {
67         Executors.newSingleThreadExecutor().execute(command);
68     }
69 }
70