/// <summary>Submit tasks using a JPPFExecutorService</summary> /// <param name="client">The JPPF client connected to the server</param> public static void SubmitWithExecutor(JPPFClient client) { JPPFExecutorService executor = new JPPFExecutorService(client); // send tasks 1 at a time executor.setBatchSize(1); IList <Future> futures = new List <Future>(); for (int i = 0; i < 3; i++) { futures.Add(executor.Submit(new MyDotnetTask(100))); } // process the results in the order the tasks were submitted foreach (Future future in futures) { // future.get() returns the value of myTask.Result after execution // or throws an eventual exception that was raised try { object result = future.get(); if (result != null) { Console.WriteLine("[executor service] got result = " + result); } else { Console.WriteLine("[executor service] no result or exception"); } } catch (Exception e) { Console.WriteLine("[executor service] exception during execution: " + e.ToString()); } } executor.shutdownNow(); }
/// <summary>Submit tasks using a JPPFCompletionService</summary> /// <param name="client">The JPPF client connected to the server</param> public static void SubmitWithCompletionService(JPPFClient client) { JPPFExecutorService executor = new JPPFExecutorService(client); JPPFCompletionService completionService = new JPPFCompletionService(executor); // send tasks 3 at a time executor.setBatchSize(3); for (int i = 0; i < 3; i++) { completionService.Submit(new MyDotnetTask(100)); } // process the results in the order in which they arrive int count = 0; while (count < 3) { // get the next completed task Future future = completionService.poll(); count++; // future.get() returns the value of myTask.Result after execution // or throws an eventual exception that was raised try { object result = future.get(); if (result != null) { Console.WriteLine("[executor service] got result = " + result); } else { Console.WriteLine("[executor service] no result or exception"); } } catch (Exception e) { Console.WriteLine("[executor service] exception during execution: " + e.ToString()); } } executor.shutdownNow(); }