Package com.packtpub.java7.concurrency.chapter7.recipe05.task

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


   
    /*
     * Create and submit to the executor 10 tasks
     */
    for (int i=0; i<10; i++) {
      SleepTwoSecondsTask task=new SleepTwoSecondsTask();
      Future<String> result=myExecutor.submit(task);
      results.add(result);
    }
   
    /*
 
View Full Code Here


    /*
     * Send four task to the executor
     */
    for (int i=0; i<4; i++){
      MyPriorityTask task=new MyPriorityTask("Task "+i,i);
      executor.execute(task);
    }
   
    /*
     * sleep the thread during one second
     */
    try {
      TimeUnit.SECONDS.sleep(1);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
   
    /*
     * Send four tasks to the executor
     */
    for (int i=4; i<8; i++) {
      MyPriorityTask task=new MyPriorityTask("Task "+i,i);
      executor.execute(task);     
    }
   
    /*
     * Shutdown the executor
View Full Code Here

    MyThreadFactory myFactory=new MyThreadFactory("MyThreadFactory");
 
    /*
     * Crate a Task
     */
    MyTask task=new MyTask();
   
    /*
     * Create a Thread using the Factory to execute the Task
     */
    Thread thread=myFactory.newThread(task);
View Full Code Here

   */
  public static void main(String[] args) throws Exception {
    /*
     * Create a Factory
     */
    MyThreadFactory myFactory=new MyThreadFactory("MyThreadFactory");
 
    /*
     * Crate a Task
     */
    MyTask task=new MyTask();
   
    /*
     * Create a Thread using the Factory to execute the Task
     */
    Thread thread=myFactory.newThread(task);
   
    /*
     * Start the Thread
     */
    thread.start();
 
View Full Code Here

    ExecutorService executor=Executors.newCachedThreadPool(threadFactory);
   
    /*
     * Create a new Task object
     */
    MyTask task=new MyTask();
   
    /*
     * Submit the task
     */
    executor.submit(task);
 
View Full Code Here

  public static void main(String[] args) throws Exception {
   
    /*
     * Create a new MyThreadFactory object
     */
    MyThreadFactory threadFactory=new MyThreadFactory("MyThreadFactory");
   
    /*
     * Create a new ThreadPoolExecutor and configure it for use the
     * MyThreadFactoryObject created before as the factory to create the threads
     */
 
View Full Code Here

  public static void main(String[] args) throws Exception {
   
    /*
     * Create a MyScheduledThreadPool object
     */
    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());
   
    /*
     * Send to the executor a delayed task. It will begin its execution after 1 second of dealy
     * and then it will be executed every three seconds
     */
    executor.scheduleAtFixedRate(task, 1, 3, TimeUnit.SECONDS);
   
    /*
     * Sleep the thread during ten seconds
     */
    TimeUnit.SECONDS.sleep(10);

    /*
     * Shutdown the executor
     */
    executor.shutdown();
   
    /*
     * Wait for the finalization of the executor
     */
    executor.awaitTermination(1, TimeUnit.DAYS);
   
    /*
     * Write a message indicating the end of the program
     */
    System.out.printf("Main: End of the program.\n");
View Full Code Here

    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

    }
   
    /*
     * Create a new Task to sum the elements of the array
     */
    MyRecursiveTask task=new MyRecursiveTask(array,0,array.length);
   
    /*
     * Send the task to the ForkJoinPool
     */
    pool.execute(task);
   
   
    /*
     * Wait for the finalization of the task
     */
    task.join();
   
    /*
     * Shutdown the pool
     */
    pool.shutdown();
   
    /*
     * Wait for the finalization of the pool
     */
    pool.awaitTermination(1, TimeUnit.DAYS);
   
    /*
     * Write the result of the task
     */
    System.out.printf("Main: Result: %d\n",task.get());
   
    /*
     * Write a message indicating the end of the program
     */
    System.out.printf("Main: End of the program\n");
View Full Code Here

  public static void main(String[] args) throws Exception {

    /*
     * Create a new MyWorkerThreadFactory
     */
    MyWorkerThreadFactory factory=new MyWorkerThreadFactory();
   
    /*
     * Create new ForkJoinPool, passing as parameter the factory created before
     */
    ForkJoinPool pool=new ForkJoinPool(4, factory, null, false);
View Full Code Here

TOP

Related Classes of com.packtpub.java7.concurrency.chapter7.recipe05.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.