From 1f3f17c8e6fd09b153a52aa7071b4744fbcd72cd Mon Sep 17 00:00:00 2001 From: zzz <zzz@mail.i2p> Date: Mon, 4 Jul 2011 16:13:28 +0000 Subject: [PATCH] * PeerManager: Load profiles in separate thread to avoid slowing down the context initAll() --- .../i2p/router/peermanager/PeerManager.java | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/router/java/src/net/i2p/router/peermanager/PeerManager.java b/router/java/src/net/i2p/router/peermanager/PeerManager.java index f27a4c46f1..ab099c129f 100644 --- a/router/java/src/net/i2p/router/peermanager/PeerManager.java +++ b/router/java/src/net/i2p/router/peermanager/PeerManager.java @@ -53,9 +53,8 @@ class PeerManager { private static final long REORGANIZE_TIME_LONG = 551*1000; /** - * Warning - this loads all the profiles in the constructor. - * This may take a long time - 30 seconds or more. - * Instantiate this in a Job or Thread. + * Profiles are now loaded in a separate thread, + * so this should return quickly. */ public PeerManager(RouterContext context) { _context = context; @@ -67,10 +66,10 @@ class PeerManager { _peersByCapability = new Set[26]; for (int i = 0; i < _peersByCapability.length; i++) _peersByCapability[i] = new ConcurrentHashSet(); - loadProfiles(); + loadProfilesInBackground(); ////_context.jobQueue().addJob(new EvaluateProfilesJob(_context)); //SimpleScheduler.getInstance().addPeriodicEvent(new Reorg(), 0, REORGANIZE_TIME); - new Reorg(); + //new Reorg(); //_context.jobQueue().addJob(new PersistProfilesJob(_context, this)); } @@ -124,6 +123,30 @@ class PeerManager { _persistenceHelper.writeProfile(prof); } + /** + * Load the profiles in a separate thread, so we don't spend + * forever in the constructor (slowing down the Router constructor + * via RouterContext.initAll()). + * This also instantiates Reorg, so only call this once + * + * @since 0.8.8 + */ + private void loadProfilesInBackground() { + (new Thread(new ProfileLoader())).start(); + } + + /** + * Load the profiles and instantiate Reorg + * + * @since 0.8.8 + */ + private class ProfileLoader implements Runnable { + public void run() { + loadProfiles(); + new Reorg(); + } + } + /** * This may take a long time - 30 seconds or more */ -- GitLab