Skip to content

Commit

Permalink
Use nanoTime for elapsed time measurement
Browse files Browse the repository at this point in the history
Signed-off-by: BJ Hargrave <[email protected]>
  • Loading branch information
bjhargrave committed Jan 12, 2023
1 parent 3c3953c commit a744964
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.net.URL;
import java.util.Enumeration;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.Test;

Expand All @@ -38,8 +39,8 @@ public void happycase() throws Exception {
.get(osgi);
assertEquals(wrapper.file, osgi);

Thread.sleep(100);
ac.purge(System.currentTimeMillis());
Thread.sleep(100L);
ac.purge(0L);
synchronized (wrapper) {
assertNull(wrapper.jarFile);
}
Expand Down Expand Up @@ -120,8 +121,8 @@ public void testWorkspace() throws Exception {
Wrapper wrapper = cl.wrappers.get()
.get(IO.getFile("jar/thinlet.jar"));

cl.autopurge(10);
Thread.sleep(500);
cl.autopurge(TimeUnit.MILLISECONDS.toNanos(10L));
Thread.sleep(500L);
synchronized (wrapper) {
assertNull(wrapper.jarFile);
}
Expand Down
29 changes: 16 additions & 13 deletions biz.aQute.bndlib/src/aQute/bnd/osgi/ActivelyClosingClassLoader.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package aQute.bnd.osgi;

import static java.util.Collections.enumeration;
import static aQute.lib.collections.Enumerations.enumeration;
import static java.util.stream.Collectors.toList;

import java.io.Closeable;
Expand Down Expand Up @@ -69,7 +69,7 @@ synchronized byte[] getData(String name) {
if (resource == null) {
return null;
}
lastAccess = System.currentTimeMillis();
lastAccess = System.nanoTime();
return IO.read(resource.openInputStream());
} catch (Exception e) {
processor.exception(e, "while loading resource %s from %s: %s", name, file, e.getMessage());
Expand Down Expand Up @@ -157,23 +157,21 @@ public String getContentType() {

@Override
public Enumeration<URL> findResources(String name) {
List<URL> resources = dataStream(name).map(data -> createURL(name, data))
.collect(toList());
return enumeration(resources);
return enumeration(dataStream(name).spliterator(), data -> createURL(name, data));
}

/**
* This method will close any open files that have not been accessed since
* purgeTime
* This method will close any open files that have not been accessed within
* the specified delta cutoff time.
*
* @param purgeTime the absolute cutoff time
* @param purgeTime The delta cutoff time in nanoseconds.
*/

void purge(long purgeTime) {
long now = System.nanoTime();
wrappers.get()
.values()
.stream()
.filter(w -> w.lastAccess < purgeTime)
.filter(w -> now - w.lastAccess >= purgeTime)
.forEach(Wrapper::close);
}

Expand Down Expand Up @@ -235,11 +233,16 @@ public Class<?> loadClass(String name) throws ClassNotFoundException {
}
}

/**
* This method will schedule closing any open files that have not been
* accessed within the specified time interval.
*
* @param freshPeriod The time interval in nanoseconds.
*/
void autopurge(long freshPeriod) {
schedule = Processor.getScheduledExecutor()
.scheduleWithFixedDelay(() -> {
purge(System.currentTimeMillis() - freshPeriod);
}, freshPeriod, freshPeriod, TimeUnit.MILLISECONDS);
.scheduleWithFixedDelay(() -> purge(freshPeriod), freshPeriod, freshPeriod,
TimeUnit.NANOSECONDS);
}

}
2 changes: 1 addition & 1 deletion biz.aQute.bndlib/src/aQute/bnd/osgi/Processor.java
Original file line number Diff line number Diff line change
Expand Up @@ -1476,7 +1476,7 @@ private CloseableMemoize<CL> newPluginLoader() {
return CloseableMemoize.closeableSupplier(() -> {
CL pluginLoader = new CL(this);
if (IO.isWindows() && isInteractive()) {
pluginLoader.autopurge(5000);
pluginLoader.autopurge(TimeUnit.SECONDS.toNanos(5L));
}
return pluginLoader;
});
Expand Down

0 comments on commit a744964

Please sign in to comment.