Package com.packtpub.java7.concurrency.chapter1.recipe7.task

Examples of com.packtpub.java7.concurrency.chapter1.recipe7.task.WriterTask


   * Main method of the example
   * @param args
   */
  public static void main(String[] args) {
    // Create the server
    Server server=new Server();
   
    // Send 100 request to the server and finish
    for (int i=0; i<100; i++){
      Task task=new Task("Task "+i);
      server.executeTask(task);
    }
   
    server.endServer();

  }
View Full Code Here


    // Create the server
    Server server=new Server();
   
    // Send 100 request to the server and finish
    for (int i=0; i<100; i++){
      Task task=new Task("Task "+i);
      server.executeTask(task);
    }
   
    server.endServer();
View Full Code Here

    ExecutorService executor=(ExecutorService)Executors.newCachedThreadPool();
   
    //Create five tasks
    ResultTask resultTasks[]=new ResultTask[5];
    for (int i=0; i<5; i++) {
      ExecutableTask executableTask=new ExecutableTask("Task "+i);
      resultTasks[i]=new ResultTask(executableTask);
      executor.submit(resultTasks[i]);
    }
   
    // Sleep the thread five seconds
View Full Code Here

  public static void main(String[] args) {
    // Create an executor
    ExecutorService executor=(ExecutorService)Executors.newCachedThreadPool();
   
    //Create five tasks
    ResultTask resultTasks[]=new ResultTask[5];
    for (int i=0; i<5; i++) {
      ExecutableTask executableTask=new ExecutableTask("Task "+i);
      resultTasks[i]=new ResultTask(executableTask);
      executor.submit(resultTasks[i]);
    }
   
    // Sleep the thread five seconds
    try {
View Full Code Here

    ReportRequest onlineRequest=new ReportRequest("Online", service);
    Thread faceThread=new Thread(faceRequest);
    Thread onlineThread=new Thread(onlineRequest);
   
    // Create a ReportSender object and a Thread to execute  it
    ReportProcessor processor=new ReportProcessor(service);
    Thread senderThread=new Thread(processor);
   
    // Start the Threads
    System.out.printf("Main: Starting the Threads\n");
    faceThread.start();
    onlineThread.start();
    senderThread.start();
   
    // Wait for the end of the ReportGenerator tasks
    try {
      System.out.printf("Main: Waiting for the report generators.\n");
      faceThread.join();
      onlineThread.join();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
   
    // Shutdown the executor
    System.out.printf("Main: Shuting down the executor.\n");
    executor.shutdown();
    try {
      executor.awaitTermination(1, TimeUnit.DAYS);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    // End the execution of the ReportSender
    processor.setEnd(true);
    System.out.printf("Main: Ends\n");

  }
View Full Code Here

    // Create the executor and thee CompletionService using that executor
    ExecutorService executor=(ExecutorService)Executors.newCachedThreadPool();
    CompletionService<String> service=new ExecutorCompletionService<>(executor);

    // Crete two ReportRequest objects and two Threads to execute them
    ReportRequest faceRequest=new ReportRequest("Face", service);
    ReportRequest onlineRequest=new ReportRequest("Online", service);
    Thread faceThread=new Thread(faceRequest);
    Thread onlineThread=new Thread(onlineRequest);
   
    // Create a ReportSender object and a Thread to execute  it
    ReportProcessor processor=new ReportProcessor(service);
View Full Code Here

    // Create a random number generator
    Random random=new Random();
    // Create and send to the executor the ten tasks
    for (int i=0; i<10; i++){
      Integer number=new Integer(random.nextInt(10));
      FactorialCalculator calculator=new FactorialCalculator(number);
      Future<Integer> result=executor.submit(calculator);
      resultList.add(result);
    }
   
    // Wait for the finalization of the ten tasks
View Full Code Here

    // Create two user validation objects
    UserValidator ldapValidator=new UserValidator("LDAP");
    UserValidator dbValidator=new UserValidator("DataBase");
   
    // Create two tasks for the user validation objects
    TaskValidator ldapTask=new TaskValidator(ldapValidator, username, password);
    TaskValidator dbTask=new TaskValidator(dbValidator,username,password);
   
    // Add the two tasks to a list of tasks
    List<TaskValidator> taskList=new ArrayList<>();
    taskList.add(ldapTask);
    taskList.add(dbTask);
View Full Code Here

    // Initialize the parameters of the user
    String username="test";
    String password="test";
   
    // Create two user validation objects
    UserValidator ldapValidator=new UserValidator("LDAP");
    UserValidator dbValidator=new UserValidator("DataBase");
   
    // Create two tasks for the user validation objects
    TaskValidator ldapTask=new TaskValidator(ldapValidator, username, password);
    TaskValidator dbTask=new TaskValidator(dbValidator,username,password);
   
View Full Code Here

    // Writes the results to the console
    System.out.printf("Core: Printing the results\n");
    for (int i=0; i<resultList.size(); i++){
      Future<Result> future=resultList.get(i);
      try {
        Result result=future.get();
        System.out.printf("%s: %s\n",result.getName(),result.getValue());
      } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
      }
    }
  }
View Full Code Here

TOP

Related Classes of com.packtpub.java7.concurrency.chapter1.recipe7.task.WriterTask

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.