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

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


  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

  public static void main(String[] args) {

    /*
     * Create a new MyLock object
     */
    MyLock lock=new MyLock();
   
    /*
     * 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();
    }
   
    /*
     * The main thread also tries to get the lock
     */
    boolean value;
    do {
      try {
        value=lock.tryLock(1,TimeUnit.SECONDS);
        if (!value) {
          System.out.printf("Main: Trying to get the Lock\n");
        }
      } catch (InterruptedException e) {
        e.printStackTrace();
        value=false;
      }
    } while (!value);
   
    /*
     * The main thread release the lock
     */
    System.out.printf("Main: Got the lock\n");
    lock.unlock();
   
    /*
     * Write a message in the console indicating the end of the program
     */
    System.out.printf("Main: End of the program\n");
View Full Code Here

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

    }

    /*
     * Create and launch the consumer
     */
    Consumer consumer=new Consumer(buffer);
    Thread consumerThread=new Thread(consumer);
    consumerThread.start();

    /*
     * Write in the console the actual consumer count
View Full Code Here

    System.out.printf("Main: Buffer: Consumer count: %d\n",buffer.getWaitingConsumerCount());

    /*
     * Transfer an event to the consumer
     */
    Event myEvent=new Event("Core Event",0);
    buffer.transfer(myEvent);
    System.out.printf("Main: My Event has ben transfered.\n");
   
    /*
     * Wait for the finalization of the producers
     */
    for (int i=0; i<producerThreads.length; i++) {
      producerThreads[i].join();
    }
   
    /*
     * Sleep the thread for one second
     */
    TimeUnit.SECONDS.sleep(1);
   
    /*
     * Write the actual consumer count
     */
    System.out.printf("Main: Buffer: Consumer count: %d\n",buffer.getWaitingConsumerCount());
   
    /*
     * Transfer another event
     */
    myEvent=new Event("Core Event 2",0);
    buffer.transfer(myEvent);
   
    /*
     * Wait for the finalization of the consumer
     */
 
View Full Code Here

    MyPriorityTransferQueue<Event> buffer=new MyPriorityTransferQueue<>();
   
    /*
     * Create a Producer object
     */
    Producer producer=new Producer(buffer);
   
    /*
     * Launch 10 producers
     */
    Thread producerThreads[]=new Thread[10];
View Full Code Here

   */
  public static void main(String[] args) throws Exception{
    /*
     * Create a ParkingCounter object
     */
    ParkingCounter counter=new ParkingCounter(5);
   
    /*
     * Create and launch two sensors
     */
    Sensor1 sensor1=new Sensor1(counter);
    Sensor2 sensor2=new Sensor2(counter);
   
    Thread thread1=new Thread(sensor1);
    Thread thread2=new Thread(sensor2);
   
    thread1.start();
    thread2.start();
   
    /*
     * Wait for the finalization of the threads
     */
    thread1.join();
    thread2.join();
   
    /*
     * Write in the console the number of cars in the parking
     */
    System.out.printf("Main: Number of cars: %d\n",counter.get());
   
    /*
     * Writ a message indicating the end of the program
     */
    System.out.printf("Main: End of the program.\n");
View Full Code Here

TOP

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