Examples of Task


Examples of com.packtpub.java7.concurrency.chapter5.recipe01.task.Task

    // 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

Examples of com.packtpub.java7.concurrency.chapter5.recipe04.task.Task

   */
  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

Examples of com.packtpub.java7.concurrency.chapter6.recipe04.task.Task

   
    /*
     * Create the five threads to execute five tasks
     */
    for (int i=0; i<taskThreads.length; i++){
      Task task=new Task(i,queue);
      taskThreads[i]=new Thread(task);
    }
   
    /*
     * Start the five threads
View Full Code Here

Examples of com.packtpub.java7.concurrency.chapter6.recipe05.task.Task

   
    /*
     * Create the five tasks
     */
    for (int i=0; i<threads.length; i++){
      Task task=new Task(i+1, queue);
      threads[i]=new Thread(task);
    }

    /*
     * Execute the five tasks
View Full Code Here

Examples of com.packtpub.java7.concurrency.chapter6.recipe06.task.Task

   
    /*
     * Execute the 25 tasks
     */
    for (char i='A'; i<'Z'; i++) {
      Task task=new Task(map, String.valueOf(i));
      threads[counter]=new Thread(task);
      threads[counter].start();
      counter++;
    }
   
View Full Code Here

Examples of com.packtpub.java7.concurrency.chapter7.recipe05.task.Task

    MyScheduledThreadPoolExecutor executor=new MyScheduledThreadPoolExecutor(2);
   
    /*
     * Create a task object 
     */
    Task task=new Task();
   
    /*
     * Write the start date of the execution
     */
    System.out.printf("Main: %s\n",new Date());
   
    /*
     * Send to the executor a delayed task. It will be executed after 1 second of delay
     */
    executor.schedule(task, 1, TimeUnit.SECONDS);
   
    /*
     * Sleeps the thread three seconds
     */
    TimeUnit.SECONDS.sleep(3);
   
    /*
     * Create another task
     */
    task=new Task();
   
    /*
     * Write the actual date again
     */
    System.out.printf("Main: %s\n",new Date());
View Full Code Here

Examples of com.packtpub.java7.concurrency.chapter7.recipe08.task.Task

   
    /*
     * Create and run ten task objects
     */
    for (int i=0; i<10; i++){
      Task task=new Task("Task-"+i,lock);
      Thread thread=new Thread(task);
      thread.start();
    }
   
    /*
 
View Full Code Here

Examples of com.packtpub.java7.concurrency.chapter7.reciper07.task.Task

    ForkJoinPool pool=new ForkJoinPool();
   
    /*
     * Task to increment the elements of the array
     */
    Task task=new Task("Task",array,0,array.length);
   
    /*
     * Send the task to the pool
     */
    pool.invoke(task);
 
View Full Code Here

Examples of com.packtpub.java7.concurrency.chapter8.recipe01.task.Task

   */
  public static void main(String[] args) throws Exception {
    /*
     * Create a task object
     */
    Task task = new Task();
   
    /*
     * Create an array to store the threads
     */
    Thread threads[] = new Thread[5];
View Full Code Here

Examples of com.packtpub.java7.concurrency.chapter8.recipe02.task.Task

   
    /*
     * Create and start five threads
     */
    for (int i=0; i<5; i++) {
      Task task=new Task(lock);
      threads[i]=new Thread(task);
      threads[i].start();
    }
   
    /*
 
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.