Small perf improvement : initialize threads names early when possible

Initializing Thread names using the Thread constructor parameter is
faster as it already sets a thread name even if no customized one is
given, while an additional call to the Thread.setName() function
internally do synchronized access, eventually runs access check on the
security manager and performs a native call.

Profiling a running YaCy server revealed that the total processing time
spent on Thread.setName() for a typical p2p search was in the range of
seconds.
pull/137/head
luccioman 7 years ago
parent 79bd9f623a
commit fa4399d5d2

@ -49,10 +49,9 @@ public class SRURSSConnector {
final CacheStrategy verify,
final boolean global,
final ClientIdentification.Agent agent) {
final Thread job = new Thread() {
final Thread job = new Thread("searchSRURSS:" + urlBase) {
@Override
public void run() {
Thread.currentThread().setName("searchSRURSS:" + urlBase);
int startRecord = 0;
RSSMessage message;
int maximumRecords = maximumRecordsInit;

@ -206,10 +206,9 @@ public abstract class AbstractSolrConnector implements SolrConnector {
final long endtime = maxtime < 0 || maxtime == Long.MAX_VALUE ? Long.MAX_VALUE : System.currentTimeMillis() + maxtime; // we know infinity!
final Thread[] t = new Thread[concurrency];
for (int i = 0; i < Math.max(1, concurrency); i++) {
t[i] = new Thread() {
t[i] = new Thread("AbstractSolrConnector:concurrentDocumentsByQueriesWithPrefetch(" + querystrings.size() + " queries, first: " + querystrings.iterator().next() + ")") {
@Override
public void run() {
this.setName("AbstractSolrConnector:concurrentDocumentsByQueriesWithPrefetch(" + querystrings.size() + " queries, first: " + querystrings.iterator().next() + ")");
String nextID;
try {
while (System.currentTimeMillis() < endtime && (nextID = idQueue.take()) != AbstractSolrConnector.POISON_ID) {

@ -357,20 +357,18 @@ public class MirrorSolrConnector extends AbstractSolrConnector implements SolrCo
return this.solr1.getCountByQuery(querystring);
}
final AtomicLong count = new AtomicLong(0);
Thread t0 = new Thread() {
Thread t0 = new Thread("MirrorSolrConnector.getCountByQuery/t0") {
@Override
public void run() {
this.setName("MirrorSolrConnector.getCountByQuery/t0");
try {
count.addAndGet(MirrorSolrConnector.this.solr0.getCountByQuery(querystring));
} catch (final IOException e) {}
}
};
t0.start();
Thread t1 = new Thread() {
Thread t1 = new Thread("MirrorSolrConnector.getCountByQuery/t1") {
@Override
public void run() {
this.setName("MirrorSolrConnector.getCountByQuery/t1");
try {
count.addAndGet(MirrorSolrConnector.this.solr1.getCountByQuery(querystring));
} catch (final IOException e) {}

@ -388,10 +388,9 @@ public class ServerShard extends SolrClient {
final Collection<QueryResponse> qrl = new ConcurrentLinkedQueue<QueryResponse>();
List<Thread> t = new ArrayList<Thread>();
for (final SolrClient s: qs) {
Thread t0 = new Thread() {
Thread t0 = new Thread("ServerShard.query/1(" + params.toString() + ")") {
@Override
public void run() {
this.setName("ServerShard.query/1(" + params.toString() + ")");
QueryResponse rsp;
try {
rsp = s.query(params);
@ -427,10 +426,9 @@ public class ServerShard extends SolrClient {
// concurrently call all shards
List<Thread> t = new ArrayList<Thread>();
for (final SolrClient s: qs) {
Thread t0 = new Thread() {
Thread t0 = new Thread("ServerShard.query/2(" + params.toString() + ")") {
@Override
public void run() {
this.setName("ServerShard.query/2(" + params.toString() + ")");
QueryResponse rsp;
try {
rsp = s.query(params, method);

@ -121,10 +121,9 @@ public class Domains {
// if such a lookup blocks, it can cause that the static initiatializer does not finish fast
// therefore we start the host name lookup as concurrent thread
// meanwhile the host name is "127.0.0.1" which is not completely wrong
new Thread() {
new Thread("Domains: init") {
@Override
public void run() {
Thread.currentThread().setName("Domains: init");
// try to get local addresses from interfaces
try {
final Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();

@ -2556,11 +2556,10 @@ public class FTPClient {
ftpClient.open(host, port);
ftpClient.login(user, pw);
final LinkedBlockingQueue<entryInfo> queue = new LinkedBlockingQueue<entryInfo>();
new Thread() {
new Thread("FTP.sitelist(" + host + ":" + port + ")") {
@Override
public void run() {
try {
Thread.currentThread().setName("FTP.sitelist(" + host + ":" + port + ")");
sitelist(ftpClient, path, queue, depth);
ftpClient.quit();
} catch (final Exception e) {} finally {

@ -1122,8 +1122,7 @@ public class HTTPClient {
private volatile boolean shutdown;
public IdleConnectionMonitorThread(HttpClientConnectionManager connMgr) {
super();
this.setName("HTTPClient.IdleConnectionMonitorThread");
super("HTTPClient.IdleConnectionMonitorThread");
this.connMgr = connMgr;
}

@ -63,9 +63,13 @@ public class Array {
}
private static class SortJobWorker extends Thread {
public SortJobWorker() {
super("Array.SortJobWorker");
}
@Override
public void run() {
this.setName("Array.SortJobWorker");
SortJob<?> job;
try {
while ((job = sortJobs.take()) != POISON_JOB_WORKER) {

@ -80,10 +80,9 @@ public class Files {
final BlockingQueue<String> q = new LinkedBlockingQueue<String>();
final InputStream is = read(f);
final BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
Thread t = new Thread() {
Thread t = new Thread("Files.concurrentLineReader:" + f) {
@Override
public void run() {
Thread.currentThread().setName("Files.concurrentLineReader:" + f);
String line;
try {
while ((line = br.readLine()) != null) {

@ -271,10 +271,9 @@ public final class CrawlStacker implements WorkflowTask<Request>{
final String host = ftpURL.getHost();
final int port = ftpURL.getPort();
final int pathParts = ftpURL.getPaths().length;
new Thread() {
new Thread("enqueueEntriesFTP") {
@Override
public void run() {
Thread.currentThread().setName("enqueueEntriesFTP");
BlockingQueue<FTPClient.entryInfo> queue;
try {
queue = FTPClient.sitelist(host, port, user, pw, ftpURL.getPath(), profile.depth());

@ -148,10 +148,9 @@ public final class Cache {
// We do this as a concurrent job only once after start-up silently
if (responseHeaderDB.size() != fileDB.size()) {
ConcurrentLog.warn("Cache", "file and metadata size is not equal, starting a cleanup thread...");
Thread startupCleanup = new Thread() {
Thread startupCleanup = new Thread("Cache startupCleanup") {
@Override
public void run() {
Thread.currentThread().setName("Cache startupCleanup");
// enumerate the responseHeaderDB and find out all entries that are not inside the fileDBunbuffered
BlockingQueue<byte[]> q = responseHeaderDB.keyQueue(1000);
final HandleSet delkeys = new RowHandleSet(Word.commonHashLength, Base64Order.enhancedCoder, 1);

@ -211,10 +211,9 @@ public class Transactions {
final String urls = url.toNormalform(true);
final File pdfPath = Transactions.definePath(url, depth, date, "pdf", Transactions.State.INVENTORY);
if (concurrency && executorRunning.intValue() < Runtime.getRuntime().availableProcessors()) {
Thread t = new Thread(){
Thread t = new Thread("Transactions.store"){
@Override
public void run() {
this.setName("Transactions.store");
executorRunning.incrementAndGet();
try {
Html2Image.writeWkhtmltopdf(urls, proxy, ClientIdentification.browserAgent.userAgent, acceptLanguage, pdfPath);

@ -235,10 +235,9 @@ public class RobotsTxt {
return;
}
if (robotsTable != null && robotsTable.containsKey(robotsTable.encodedKey(urlHostPort))) return;
Thread t = new Thread() {
Thread t = new Thread("Robots.txt:ensureExist(" + theURL.toNormalform(true) + ")") {
@Override
public void run(){
this.setName("Robots.txt:ensureExist(" + theURL.toNormalform(true) + ")");
// make or get a synchronization object
DomSync syncObj = RobotsTxt.this.syncObjects.get(urlHostPort);
if (syncObj == null) {

@ -177,10 +177,9 @@ public class PhpBB3Dao implements Dao {
private BlockingQueue<DCEntry> toQueue(final StringBuilder sql, int queueSize) {
// execute the query and push entries to a queue concurrently
final BlockingQueue<DCEntry> queue = new ArrayBlockingQueue<DCEntry>(queueSize);
Thread dbreader = new Thread() {
Thread dbreader = new Thread("PhpBB3Dao.toQueue") {
@Override
public void run() {
Thread.currentThread().setName("PhpBB3Dao.toQueue");
Statement stmt = null;
ResultSet rs = null;
try {

@ -206,10 +206,9 @@ public class pdfParser extends AbstractParser implements Parser {
stripper.setEndPage(Integer.MAX_VALUE); // set to default
// we start the pdf parsing in a separate thread to ensure that it can be terminated
final PDDocument pdfDocC = pdfDoc;
final Thread t = new Thread() {
final Thread t = new Thread("pdfParser.getText:" + location) {
@Override
public void run() {
Thread.currentThread().setName("pdfParser.getText:" + location);
try {
writer.append(stripper.getText(pdfDocC));
} catch (final Throwable e) {}

@ -164,10 +164,9 @@ public class YaCyApp {
// registering shutdown hook
log.info("Registering Shutdown Hook");
Thread t = new Thread() {
Thread t = new Thread("YaCyApp") {
@Override
public void run() {
Thread.currentThread().setName("YaCyApp");
app = new Application("YaCy GUI", operation, menues, new InfoPage(host, port));
app.setLocationRelativeTo(null);
app.setVisible(true);

@ -298,11 +298,10 @@ public class Jetty9HttpServerImpl implements YaCyHttpServer {
@Override
public void reconnect(final int milsec) {
new Thread() {
new Thread("Jetty8HttpServer.reconnect") {
@Override
public void run() {
this.setName("Jetty8HttpServer.reconnect");
try {
Thread.sleep(milsec);
} catch (final InterruptedException e) {

@ -526,10 +526,9 @@ public class MapHeap implements Map<byte[], Map<String, String>> {
public final static byte[] POISON_QUEUE_ENTRY = "POISON".getBytes();
public BlockingQueue<byte[]> keyQueue(final int size) {
final ArrayBlockingQueue<byte[]> set = new ArrayBlockingQueue<byte[]>(size);
(new Thread() {
(new Thread("MapHeap.keyQueue:" + size) {
@Override
public void run() {
Thread.currentThread().setName("MapHeap.keyQueue:" + size);
try {
final Iterator<byte[]> i = MapHeap.this.blob.keys(true, false);
while (i.hasNext())

@ -58,13 +58,13 @@ public class IODispatcher extends Thread {
private final int writeBufferSize;
public IODispatcher(final int dumpQueueLength, final int mergeQueueLength, final int writeBufferSize) {
super("IODispatcher");
this.termination = new Semaphore(0);
this.controlQueue = new Semaphore(0);
this.dumpQueue = new ArrayBlockingQueue<DumpJob<? extends Reference>>(dumpQueueLength);
this.mergeQueue = new ArrayBlockingQueue<MergeJob>(mergeQueueLength);
this.writeBufferSize = writeBufferSize;
this.terminate = false;
this.setName("IODispatcher");
}
public void terminate() {

@ -113,8 +113,9 @@ public final class IndexCell<ReferenceType extends Reference> extends AbstractBu
private class FlushThread extends Thread {
public FlushThread(String name) {
this.setName("IndexCell.FlushThread(" + name + ")");
super("IndexCell.FlushThread(" + name + ")");
}
@Override
public void run() {
while (IndexCell.this.flushShallRun) {

@ -231,10 +231,9 @@ public class SplitTable implements Index, Iterable<Row.Entry> {
}
}
final Table a = table;
final Thread p = new Thread() {
final Thread p = new Thread("SplitTable.warmUp") {
@Override
public void run() {
Thread.currentThread().setName("SplitTable.warmUp");
a.warmUp();
}
};

@ -331,10 +331,9 @@ public class RemoteSearch extends Thread {
// prepare seed targets and threads
final Seed targetPeer = event.peers.getConnected(targethash);
if (targetPeer == null || targetPeer.hash == null) return null;
Thread secondary = new Thread() {
Thread secondary = new Thread("RemoteSearch.secondaryRemoteSearch(" + wordhashes + " to " + targethash + ")") {
@Override
public void run() {
this.setName("RemoteSearch.secondaryRemoteSearch(" + wordhashes + " to " + targethash + ")");
event.oneFeederStarted();
try {
int urls = Protocol.secondarySearch(
@ -400,10 +399,9 @@ public class RemoteSearch extends Thread {
// check own peer status
if (event.peers.mySeed() == null) { return null; }
// prepare threads
Thread solr = new Thread() {
Thread solr = new Thread("RemoteSearch.solrRemoteSearch(" + solrQuery.getQuery() + " to " + (targetPeer == null ? "myself" : targetPeer.hash) + ")") {
@Override
public void run() {
this.setName("RemoteSearch.solrRemoteSearch(" + solrQuery.getQuery() + " to " + (targetPeer == null ? "myself" : targetPeer.hash) + ")");
int urls = 0;
try {
event.oneFeederStarted();

@ -180,7 +180,7 @@ public class WebStructureGraph {
*/
private class PublicRefDNSResolvingProcess extends Thread {
private PublicRefDNSResolvingProcess() {
this.setName("WebStructureGraph.PublicRefDNSResolvingProcess");
super("WebStructureGraph.PublicRefDNSResolvingProcess");
}
@Override

@ -46,9 +46,9 @@ public class MemoryTracker extends Thread {
private boolean running;
public MemoryTracker(final long time) {
super("MemoryTracker");
this.delaytime = time;
running = true;
this.setName("MemoryTracker");
}
@Override

@ -416,10 +416,9 @@ public final class Switchboard extends serverSwitch {
// init libraries
this.log.config("initializing libraries");
new Thread() {
new Thread("LibraryProvider.initialize") {
@Override
public void run() {
Thread.currentThread().setName("LibraryProvider.initialize");
LibraryProvider.initialize(Switchboard.this.dictionariesPath);
// persistent Vocabulary Switches
final Set<String> omit = Switchboard.this.getConfigSet("search.result.show.vocabulary.omit");
@ -857,10 +856,9 @@ public final class Switchboard extends serverSwitch {
// init bookmarks DB: needs more time since this does a DNS lookup for each Bookmark.
// Can be started concurrently
new Thread() {
new Thread("Switchboard.initBookmarks") {
@Override
public void run() {
Thread.currentThread().setName("Switchboard.initBookmarks");
try {
initBookmarks();
} catch (final IOException e ) {
@ -1281,10 +1279,9 @@ public final class Switchboard extends serverSwitch {
super.setHttpServer(server);
// finally start jobs which shall be started after start-up
new Thread() {
new Thread("Switchboard.setHttpServer") {
@Override
public void run() {
Thread.currentThread().setName("Switchboard.setHttpServer");
try {Thread.sleep(10000);} catch (final InterruptedException e) {} // needs httpd up
schedulerJob(); // trigger startup actions
}
@ -3450,10 +3447,9 @@ public final class Switchboard extends serverSwitch {
final List<Thread> stackthreads = new ArrayList<Thread>(); // do this concurrently
for (DigestURL url: rootURLs) {
final DigestURL turl = url;
Thread t = new Thread() {
Thread t = new Thread("Switchboard.stackURLs") {
@Override
public void run() {
this.setName("Switchboard.stackURLs");
String failreason;
if ((failreason = Switchboard.this.stackUrl(profile, turl)) == null) successurls.add(turl); else failurls.put(turl, failreason);
}
@ -4018,10 +4014,9 @@ public final class Switchboard extends serverSwitch {
}
public final void heuristicSite(final SearchEvent searchEvent, final String host) {
new Thread() {
new Thread("Switchboard.heuristicSite:" + host) {
@Override
public void run() {
Thread.currentThread().setName("Switchboard.heuristicSite:" + host);
String r = host;
if ( r.indexOf("//", 0) < 0 ) {
r = "http://" + r;
@ -4132,10 +4127,9 @@ public final class Switchboard extends serverSwitch {
final SearchEvent searchEvent,
final String feedName) {
new Thread() {
new Thread("heuristicRSS:" + feedName) {
@Override
public void run() {
Thread.currentThread().setName("heuristicRSS:" + feedName);
final DigestURL url;
try {
url = new DigestURL(MultiProtocolURL.unescape(urlpattern));

@ -451,11 +451,9 @@ public final class SearchEvent implements ScoreMapUpdatesListener {
// start this concurrently because the remote search needs an enumeration
// of the remote peers which may block in some cases when i.e. DHT is active
// at the same time.
new Thread() {
new Thread("SearchEvent.primaryRemoteSearches") {
@Override
public void run() {
this.setName("SearchEvent.init(" + query.getQueryGoal().getQueryString(false) + ")");
Thread.currentThread().setName("SearchEvent.primaryRemoteSearches");
RemoteSearch.primaryRemoteSearches(
SearchEvent.this,
0, remote_maxcount,

@ -48,10 +48,9 @@ public class SecondarySearchSuperviser extends Thread {
return;
}
// extend the abstracts in the cache: join the single abstracts
new Thread() {
new Thread("SecondarySearch.addAbstract:" + wordhash) {
@Override
public void run() {
Thread.currentThread().setName("SecondarySearch.addAbstract:" + wordhash);
for ( final Map.Entry<String, Set<String>> oneref : singleAbstract.entrySet() ) {
final String urlhash = oneref.getKey();
final Set<String> peerlistNew = oneref.getValue();

Loading…
Cancel
Save