@@ -91,8 +91,10 @@ public WorkRequestHandler(
9191 * @param messageProcessor Object responsible for parsing {@code WorkRequest}s from the server and
9292 * writing {@code WorkResponses} to the server.
9393 * @param cpuUsageBeforeGc The minimum amount of CPU time between explicit garbage collection
94- * calls.
94+ * calls. Pass Duration.ZERO to not do explicit garbage collection.
95+ * @deprecated Use WorkRequestHandlerBuilder instead.
9596 */
97+ @ Deprecated ()
9698 public WorkRequestHandler (
9799 BiFunction <List <String >, PrintWriter , Integer > callback ,
98100 PrintStream stderr ,
@@ -104,6 +106,48 @@ public WorkRequestHandler(
104106 this .gcScheduler = new CpuTimeBasedGcScheduler (cpuUsageBeforeGc );
105107 }
106108
109+ /** Builder class for WorkRequestHandler. Required parameters are passed to the constructor. */
110+ public static class WorkRequestHandlerBuilder {
111+ private final BiFunction <List <String >, PrintWriter , Integer > callback ;
112+ private final PrintStream stderr ;
113+ private final WorkerMessageProcessor messageProcessor ;
114+ private Duration cpuUsageBeforeGc = Duration .ZERO ;
115+
116+ /**
117+ * Creates a {@code WorkRequestHandlerBuilder}.
118+ *
119+ * @param callback Callback method for executing a single WorkRequest in a thread. The first
120+ * argument to {@code callback} is the set of command-line arguments, the second is where
121+ * all error messages and other user-oriented messages should be written to. The callback
122+ * must return an exit code indicating success (zero) or failure (nonzero).
123+ * @param stderr Stream that log messages should be written to, typically the process' stderr.
124+ * @param messageProcessor Object responsible for parsing {@code WorkRequest}s from the server
125+ * and writing {@code WorkResponses} to the server.
126+ */
127+ public WorkRequestHandlerBuilder (
128+ BiFunction <List <String >, PrintWriter , Integer > callback ,
129+ PrintStream stderr ,
130+ WorkerMessageProcessor messageProcessor ) {
131+ this .callback = callback ;
132+ this .stderr = stderr ;
133+ this .messageProcessor = messageProcessor ;
134+ }
135+
136+ /**
137+ * Sets the minimum amount of CPU time between explicit garbage collection calls. Pass
138+ * Duration.ZERO to not do explicit garbage collection (the default).
139+ */
140+ public WorkRequestHandlerBuilder setCpuUsageBeforeGc (Duration cpuUsageBeforeGc ) {
141+ this .cpuUsageBeforeGc = cpuUsageBeforeGc ;
142+ return this ;
143+ }
144+
145+ /** Returns a WorkRequestHandler instance with the values in this Builder. */
146+ public WorkRequestHandler build () {
147+ return new WorkRequestHandler (callback , stderr , messageProcessor , cpuUsageBeforeGc );
148+ }
149+ }
150+
107151 /**
108152 * Runs an infinite loop of reading {@link WorkRequest} from {@code in}, running the callback,
109153 * then writing the corresponding {@link WorkResponse} to {@code out}. If there is an error
0 commit comments