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

Examples of com.packtpub.java7.concurrency.chapter1.recipe11.task.Task


    ScheduledExecutorService executor=Executors.newScheduledThreadPool(1);
    System.out.printf("Main: Starting at: %s\n",new Date());

    // Create a new task and sends it to the executor. It will start with a delay of 1 second and then
    // it will execute every two seconds
    Task task=new Task("Task");
    ScheduledFuture<?> result=executor.scheduleAtFixedRate(task, 1, 2, TimeUnit.SECONDS);
   
    // Controlling the execution of tasks
    for (int i=0; i<10; i++){
      System.out.printf("Main: Delay: %d\n",result.getDelay(TimeUnit.MILLISECONDS));
View Full Code Here


   
    // Create an executor
    ThreadPoolExecutor executor=(ThreadPoolExecutor)Executors.newCachedThreadPool();
   
    // Create a task
    Task task=new Task();
   
    System.out.printf("Main: Executing the Task\n");

    // Send the task to the executor
    Future<String> result=executor.submit(task);
View Full Code Here

    // Create a list of products
    ProductListGenerator generator=new ProductListGenerator();
    List<Product> products=generator.generate(10000);
   
    // Craete a task
    Task task=new Task(products, 0, products.size(), 0.20);
   
    // Create a ForkJoinPool
    ForkJoinPool pool=new ForkJoinPool();
   
    // Execute the Task
    pool.execute(task);

    // Write information about the pool
    do {
      System.out.printf("Main: Thread Count: %d\n",pool.getActiveThreadCount());
      System.out.printf("Main: Thread Steal: %d\n",pool.getStealCount());
      System.out.printf("Main: Paralelism: %d\n",pool.getParallelism());
      try {
        TimeUnit.MILLISECONDS.sleep(5);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    } while (!task.isDone());
 
    // Shutdown the pool
    pool.shutdown();
   
    // Check if the task has completed normally
    if (task.isCompletedNormally()){
      System.out.printf("Main: The process has completed normally.\n");
    }

    // Expected result: 12. Write products which price is not 12
    for (int i=0; i<products.size(); i++){
View Full Code Here

  /**
   * Method that updates the prices of the assigned products to the task
   */
  private void updatePrices() {
    for (int i=first; i<last; i++){
      Product product=products.get(i);
      product.setPrice(product.getPrice()*(1+increment));
    }
  }
 
View Full Code Here

      System.out.printf("Main: The process has completed normally.\n");
    }

    // Expected result: 12. Write products which price is not 12
    for (int i=0; i<products.size(); i++){
      Product product=products.get(i);
      if (product.getPrice()!=12) {
        System.out.printf("Product %s: %f\n",product.getName(),product.getPrice());
      }
    }
   
    // End of the program
    System.out.println("Main: End of the program.\n");
View Full Code Here

   * @param args
   */
  public static void main(String[] args) {

    // Create a list of products
    ProductListGenerator generator=new ProductListGenerator();
    List<Product> products=generator.generate(10000);
   
    // Craete a task
    Task task=new Task(products, 0, products.size(), 0.20);
   
    // Create a ForkJoinPool
View Full Code Here

    // Generate a document with 100 lines and 1000 words per line
    DocumentMock mock=new DocumentMock();
    String[][] document=mock.generateDocument(100, 1000, "the");
 
    // Create a DocumentTask
    DocumentTask task=new DocumentTask(document, 0, 100, "the");
   
    // Create a ForkJoinPool
    ForkJoinPool pool=new ForkJoinPool();
   
    // Execute the Task
    pool.execute(task);
   
    // Write statistics about the pool
    do {
      System.out.printf("******************************************\n");
      System.out.printf("Main: Parallelism: %d\n",pool.getParallelism());
      System.out.printf("Main: Active Threads: %d\n",pool.getActiveThreadCount());
      System.out.printf("Main: Task Count: %d\n",pool.getQueuedTaskCount());
      System.out.printf("Main: Steal Count: %d\n",pool.getStealCount());
      System.out.printf("******************************************\n");

      try {
        TimeUnit.SECONDS.sleep(1);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
     
    } while (!task.isDone());

    // Shutdown the pool
    pool.shutdown();
   
    // Wait for the finalization of the tasks
    try {
      pool.awaitTermination(1, TimeUnit.DAYS);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
   
    // Write the results of the tasks
    try {
      System.out.printf("Main: The word appears %d in the document",task.get());
    } catch (InterruptedException | ExecutionException e) {
      e.printStackTrace();
    }
  }
View Full Code Here

   * Main method of the class
   */
  public static void main(String[] args) {
   
    // Generate a document with 100 lines and 1000 words per line
    DocumentMock mock=new DocumentMock();
    String[][] document=mock.generateDocument(100, 1000, "the");
 
    // Create a DocumentTask
    DocumentTask task=new DocumentTask(document, 0, 100, "the");
   
    // Create a ForkJoinPool
View Full Code Here

   */
  public static void main(String[] args) {
    // Array of 100 integers
    int array[]=new int[100];
    // Task to process the array
    Task task=new Task(array,0,100);
    // ForkJoinPool to execute the Task
    ForkJoinPool pool=new ForkJoinPool();
   
    // Execute the task
    pool.execute(task);
 
    // Shutdown the ForkJoinPool
    pool.shutdown();
   
    // Wait for the finalization of the task
    try {
      pool.awaitTermination(1, TimeUnit.DAYS);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
   
    // Check if the task has thrown an Exception. If it's the case, write it
    // to the console
   
    if (task.isCompletedAbnormally()) {
      System.out.printf("Main: An exception has ocurred\n");
      System.out.printf("Main: %s\n",task.getException());
    }
   
    System.out.printf("Main: Result: %d",task.join());
  }
View Full Code Here

   
    // Create a ForkJoinPool with the default constructor
    ForkJoinPool pool=new ForkJoinPool();
   
    // Create a Task to process the array
    SearchNumberTask task=new SearchNumberTask(array,0,1000,5,manager);
   
    // Execute the task
    pool.execute(task);

    // Shutdown the pool
View Full Code Here

TOP

Related Classes of com.packtpub.java7.concurrency.chapter1.recipe11.task.Task

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.