Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/async/BackgroundJobHandler.java
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2018 Erik Duisters <e.duisters1@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | package org.kde.kdeconnect.async; | ||||
22 | | ||||
23 | import android.os.Handler; | ||||
24 | import android.os.Looper; | ||||
25 | import android.support.annotation.Nullable; | ||||
26 | import android.util.Log; | ||||
27 | | ||||
28 | import java.util.HashMap; | ||||
29 | import java.util.Map; | ||||
30 | import java.util.concurrent.BlockingQueue; | ||||
31 | import java.util.concurrent.CancellationException; | ||||
32 | import java.util.concurrent.ExecutionException; | ||||
33 | import java.util.concurrent.Future; | ||||
34 | import java.util.concurrent.LinkedBlockingQueue; | ||||
35 | import java.util.concurrent.RejectedExecutionException; | ||||
36 | import java.util.concurrent.ThreadPoolExecutor; | ||||
37 | import java.util.concurrent.TimeUnit; | ||||
38 | | ||||
39 | public class BackgroundJobHandler { | ||||
40 | private static final String TAG = BackgroundJobHandler.class.getSimpleName(); | ||||
41 | | ||||
42 | private final Map<BackgroundJob, Future<?>> jobMap = new HashMap<>(); | ||||
43 | private final Object jobMapLock = new Object(); | ||||
44 | | ||||
45 | private class MyThreadPoolExecutor extends ThreadPoolExecutor { | ||||
46 | MyThreadPoolExecutor(int corePoolSize, int maxPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { | ||||
47 | super(corePoolSize, maxPoolSize, keepAliveTime, unit, workQueue); | ||||
48 | } | ||||
49 | | ||||
50 | @Override | ||||
51 | protected void afterExecute(Runnable r, Throwable t) { | ||||
52 | super.afterExecute(r, t); | ||||
53 | | ||||
54 | if (!(r instanceof Future)) { | ||||
55 | return; | ||||
56 | } | ||||
57 | | ||||
58 | Future<?> future = (Future<?>) r; | ||||
59 | | ||||
60 | if (t == null) { | ||||
61 | try { | ||||
62 | future.get(); | ||||
63 | } catch (CancellationException ce) { | ||||
64 | Log.d(TAG,"afterExecute got a CancellationException"); | ||||
65 | } catch (ExecutionException ee) { | ||||
66 | t = ee; | ||||
67 | } catch (InterruptedException ie) { | ||||
68 | Log.d(TAG, "afterExecute got an InterruptedException"); | ||||
69 | Thread.currentThread().interrupt(); // ignore/reset | ||||
70 | } | ||||
71 | } | ||||
72 | | ||||
73 | if (t != null) { | ||||
74 | BackgroundJobHandler.this.handleUncaughtException(future, t); | ||||
75 | } | ||||
76 | } | ||||
77 | } | ||||
78 | | ||||
79 | private final ThreadPoolExecutor threadPoolExecutor; | ||||
80 | private Handler handler; | ||||
81 | | ||||
82 | private BackgroundJobHandler(int corePoolSize, int maxPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { | ||||
83 | this.handler = new Handler(Looper.getMainLooper()); | ||||
84 | this.threadPoolExecutor = new MyThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, unit, workQueue); | ||||
85 | } | ||||
86 | | ||||
87 | public void runJob(BackgroundJob bgJob) { | ||||
88 | Future<?> f; | ||||
89 | | ||||
90 | bgJob.setBackgroundJobHandler(this); | ||||
91 | | ||||
92 | try { | ||||
93 | synchronized (jobMapLock) { | ||||
94 | f = threadPoolExecutor.submit(bgJob); | ||||
95 | jobMap.put(bgJob, f); | ||||
96 | } | ||||
97 | } catch (RejectedExecutionException e) { | ||||
98 | Log.d(TAG,"threadPoolExecutor.submit rejected a background job: " + e.getMessage()); | ||||
99 | | ||||
100 | bgJob.reportError(e); | ||||
101 | } | ||||
102 | } | ||||
103 | | ||||
104 | public boolean isRunning(long jobId) { | ||||
105 | synchronized (jobMapLock) { | ||||
106 | for (BackgroundJob job : jobMap.keySet()) { | ||||
107 | if (job.getId() == jobId) { | ||||
108 | return true; | ||||
109 | } | ||||
110 | } | ||||
111 | } | ||||
112 | | ||||
113 | return false; | ||||
114 | } | ||||
115 | | ||||
116 | @Nullable | ||||
117 | public BackgroundJob getJob(long jobId) { | ||||
118 | synchronized (jobMapLock) { | ||||
119 | for (BackgroundJob job : jobMap.keySet()) { | ||||
120 | if (job.getId() == jobId) { | ||||
121 | return job; | ||||
122 | } | ||||
123 | } | ||||
124 | } | ||||
125 | | ||||
126 | return null; | ||||
127 | } | ||||
128 | | ||||
129 | void cancelJob(BackgroundJob job) { | ||||
130 | synchronized (jobMapLock) { | ||||
131 | if (jobMap.containsKey(job)) { | ||||
132 | Future<?> f = jobMap.get(job); | ||||
133 | | ||||
134 | if (f.cancel(true)) { | ||||
135 | threadPoolExecutor.purge(); | ||||
136 | } | ||||
137 | | ||||
138 | jobMap.remove(job); | ||||
139 | } | ||||
140 | } | ||||
141 | } | ||||
142 | | ||||
143 | private void handleUncaughtException(Future<?> ft, Throwable t) { | ||||
144 | synchronized (jobMapLock) { | ||||
145 | for (Map.Entry<BackgroundJob, Future<?>> pairs : jobMap.entrySet()) { | ||||
146 | Future<?> future = pairs.getValue(); | ||||
147 | | ||||
148 | if (future == ft) { | ||||
149 | pairs.getKey().reportError(t); | ||||
150 | break; | ||||
151 | } | ||||
152 | } | ||||
153 | } | ||||
154 | } | ||||
155 | | ||||
156 | void onFinished(BackgroundJob job) { | ||||
157 | synchronized (jobMapLock) { | ||||
158 | jobMap.remove(job); | ||||
159 | } | ||||
160 | } | ||||
161 | | ||||
162 | void runOnUiThread(Runnable runnable) { | ||||
163 | handler.post(runnable); | ||||
164 | } | ||||
165 | | ||||
166 | public static BackgroundJobHandler newFixedThreadPoolBackgroundJobHander(int numThreads) { | ||||
167 | return new BackgroundJobHandler(numThreads, numThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>()); | ||||
168 | } | ||||
169 | } |