/hg/icedtea-web: Changed ResourceTracker to use cached thread po...
jkang at icedtea.classpath.org
jkang at icedtea.classpath.org
Tue Oct 7 19:10:42 UTC 2014
changeset 6f4c1d501560 in /hg/icedtea-web
details: http://icedtea.classpath.org/hg/icedtea-web?cmd=changeset;node=6f4c1d501560
author: Jie Kang <jkang at redhat.com>
date: Tue Oct 07 14:41:11 2014 -0400
Changed ResourceTracker to use cached thread pool ExecutorService.
2014-10-07 Jie Kang <jkang at redhat.com>
Changed ResourceTracker to use cached thread pool as opposed to manual
thread management
* netx/net/sourceforge/jnlp/cache/ResourceTracker.java:
diffstat:
ChangeLog | 6 +
netx/net/sourceforge/jnlp/cache/ResourceTracker.java | 88 ++++---------------
2 files changed, 24 insertions(+), 70 deletions(-)
diffs (212 lines):
diff -r d214c5f0fef7 -r 6f4c1d501560 ChangeLog
--- a/ChangeLog Tue Oct 07 16:51:08 2014 +0200
+++ b/ChangeLog Tue Oct 07 14:41:11 2014 -0400
@@ -1,3 +1,9 @@
+2014-10-07 Jie Kang <jkang at redhat.com>
+
+ Changed ResourceTracker to use cached thread pool as opposed to manual
+ thread management
+ * netx/net/sourceforge/jnlp/cache/ResourceTracker.java:
+
2014-10-07 Jiri Vanek <jvanek at redhat.com>
Files, arguments types and icedtea-web man page made localizable
diff -r d214c5f0fef7 -r 6f4c1d501560 netx/net/sourceforge/jnlp/cache/ResourceTracker.java
--- a/netx/net/sourceforge/jnlp/cache/ResourceTracker.java Tue Oct 07 16:51:08 2014 +0200
+++ b/netx/net/sourceforge/jnlp/cache/ResourceTracker.java Tue Oct 07 14:41:11 2014 -0400
@@ -46,6 +46,8 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
import java.util.jar.JarOutputStream;
import java.util.jar.Pack200;
import java.util.jar.Pack200.Unpacker;
@@ -126,11 +128,7 @@
/** notified on initialization or download of a resource */
private static final Object lock = new Object(); // used to lock static structures
- /** max threads */
- private static final int maxThreads = 5;
-
- /** running threads */
- private static int threads = 0;
+ private static final ExecutorService threadPool = Executors.newCachedThreadPool();
/** weak list of resource trackers with resources to prefetch */
private static final WeakList<ResourceTracker> prefetchTrackers =
@@ -142,10 +140,6 @@
private static final ConcurrentHashMap<Resource, DownloadOptions> downloadOptions =
new ConcurrentHashMap<>();
- /** resource trackers threads are working for (used for load balancing across multi-tracker downloads) */
- private final static ArrayList<ResourceTracker> active =
- new ArrayList<>(); //
-
/** the resources known about by this resource tracker */
private final List<Resource> resources = new ArrayList<>();
@@ -198,7 +192,6 @@
OutputController.getLogger().log(ex);
}
Resource resource = Resource.getResource(location, version, updatePolicy);
- boolean downloaded = false;
synchronized (resources) {
if (resources.contains(resource))
@@ -216,12 +209,12 @@
// should really be synchronized on resources, but the worst
// case should be that the resource will be updated once even
// if unnecessary.
- downloaded = checkCache(resource, updatePolicy);
+ boolean downloaded = checkCache(resource, updatePolicy);
- synchronized (lock) {
- if (!downloaded)
- if (prefetch && threads == 0) // existing threads do pre-fetch when queue empty
- startThread();
+ if (!downloaded) {
+ if (prefetch) {
+ startDownloadThread();
+ }
}
}
@@ -541,19 +534,13 @@
}
/**
- * Start a new download thread if there are not too many threads
- * already running.
+ * Start a new download thread.
* <p>
* Calls to this method should be synchronized on lock.
* </p>
*/
- protected void startThread() {
- if (threads < maxThreads) {
- threads++;
-
- Thread thread = new Thread(new Downloader(), "DownloaderThread" + threads);
- thread.start();
- }
+ protected void startDownloadThread() {
+ threadPool.execute(new Downloader());
}
/**
@@ -563,25 +550,9 @@
* </p>
*/
private void endThread() {
- threads--;
-
- if (threads < 0) {
- // this should never happen but try to recover
- threads = 0;
-
- if (queue.size() > 0) // if any on queue make sure a thread is running
- startThread(); // look into whether this could create a loop
-
- throw new RuntimeException("tracker threads < 0");
- }
-
- if (threads == 0) {
- synchronized (prefetchTrackers) {
- queue.trimToSize(); // these only accessed by threads so no sync needed
- active.clear(); // no threads so no trackers actively downloading
- active.trimToSize();
- prefetchTrackers.trimToSize();
- }
+ synchronized (prefetchTrackers) {
+ queue.trimToSize(); // these only accessed by threads so no sync needed
+ prefetchTrackers.trimToSize();
}
}
@@ -595,7 +566,7 @@
throw new IllegalResourceDescriptorException("Invalid resource state (resource: " + resource + ")");
queue.add(resource);
- startThread();
+ startDownloadThread();
}
}
@@ -1035,8 +1006,8 @@
if (result != null)
queue.remove(result);
- // prefetch if nothing found so far and this is the last thread
- if (result == null && threads == 1)
+ // prefetch if nothing found so far
+ if (result == null)
result = getPrefetch();
if (result == null)
@@ -1113,7 +1084,6 @@
static Resource selectByFilter(Collection<Resource> source, Filter<Resource> filter) {
Resource result = null;
- int score = Integer.MAX_VALUE;
for (Resource resource : source) {
boolean selectable;
@@ -1123,19 +1093,7 @@
}
if (selectable) {
- int activeCount = 0;
-
- for (ResourceTracker rt : active) {
- if (rt == resource.getTracker())
- activeCount++;
- }
-
- // try to spread out the downloads so that a slow host
- // won't monopolize the downloads
- if (activeCount < score) {
result = resource;
- score = activeCount;
- }
}
}
@@ -1268,27 +1226,18 @@
public void run() {
while (true) {
synchronized (lock) {
- // remove from active list, used for load balancing
- if (resource != null)
- active.remove(resource.getTracker());
-
resource = selectNextResource();
if (resource == null) {
endThread();
break;
}
-
- // add to active list, used for load balancing
- active.add(resource.getTracker());
}
try {
-
- // Resource processing involves writing to files
+ // Resource processing involves writing to files
// (cache entry trackers, the files themselves, etc.)
// and it therefore needs to be privileged
-
final Resource fResource = resource;
AccessController.doPrivileged(new PrivilegedAction<Void>() {
@Override
@@ -1297,7 +1246,6 @@
return null;
}
});
-
} catch (Exception ex) {
OutputController.getLogger().log(ex);
}
More information about the distro-pkg-dev
mailing list