1 11 package org.eclipse.core.internal.utils; 12 13 import java.util.*; 14 import org.eclipse.core.runtime.*; 15 import org.eclipse.core.runtime.jobs.*; 16 import org.osgi.framework.Bundle; 17 18 22 public class StringPoolJob extends Job { 23 private static final long INITIAL_DELAY = 10000; private static final long RESCHEDULE_DELAY = 300000; private long lastDuration; 26 30 private Map participants = Collections.synchronizedMap(new HashMap(10)); 31 32 private final Bundle systemBundle = Platform.getBundle("org.eclipse.osgi"); 34 public StringPoolJob() { 35 super(Messages.utils_stringJobName); 36 setSystem(true); 37 setPriority(DECORATE); 38 } 39 40 58 public void addStringPoolParticipant(IStringPoolParticipant participant, ISchedulingRule rule) { 59 participants.put(participant, rule); 60 if (getState() == Job.SLEEPING) 61 wakeUp(INITIAL_DELAY); 62 else 63 schedule(INITIAL_DELAY); 64 } 65 66 74 public void removeStringPoolParticipant(IStringPoolParticipant participant) { 75 participants.remove(participant); 76 } 77 78 81 protected IStatus run(IProgressMonitor monitor) { 82 if (systemBundle.getState() == Bundle.STOPPING) 84 return Status.OK_STATUS; 85 86 Map.Entry[] entries = (Map.Entry[]) participants.entrySet().toArray(new Map.Entry[0]); 88 ISchedulingRule[] rules = new ISchedulingRule[entries.length]; 89 IStringPoolParticipant[] toRun = new IStringPoolParticipant[entries.length]; 90 for (int i = 0; i < toRun.length; i++) { 91 toRun[i] = (IStringPoolParticipant) entries[i].getKey(); 92 rules[i] = (ISchedulingRule) entries[i].getValue(); 93 } 94 final ISchedulingRule rule = MultiRule.combine(rules); 95 long start = -1; 96 int savings = 0; 97 final IJobManager jobManager = Job.getJobManager(); 98 try { 99 jobManager.beginRule(rule, monitor); 100 start = System.currentTimeMillis(); 101 savings = shareStrings(toRun, monitor); 102 } finally { 103 jobManager.endRule(rule); 104 } 105 if (start > 0) { 106 lastDuration = System.currentTimeMillis() - start; 107 if (Policy.DEBUG_STRINGS) 108 Policy.debug("String sharing saved " + savings + " bytes in: " + lastDuration); } 110 long scheduleDelay = Math.max(RESCHEDULE_DELAY, lastDuration*100); 112 if (Policy.DEBUG_STRINGS) 113 Policy.debug("Rescheduling string sharing job in: " + scheduleDelay); schedule(scheduleDelay); 115 return Status.OK_STATUS; 116 } 117 118 private int shareStrings(IStringPoolParticipant[] toRun, IProgressMonitor monitor) { 119 final StringPool pool = new StringPool(); 120 for (int i = 0; i < toRun.length; i++) { 121 if (monitor.isCanceled()) 122 break; 123 final IStringPoolParticipant current = toRun[i]; 124 SafeRunner.run(new ISafeRunnable() { 125 public void handleException(Throwable exception) { 126 } 128 129 public void run() { 130 current.shareStrings(pool); 131 } 132 }); 133 } 134 return pool.getSavedStringCount(); 135 } 136 } 137 | Popular Tags |