Package com.packtpub.java7.concurrency.chapter6.recipe04.task

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


    Account  account=new Account();
    // an initialize its balance to 1000
    account.setBalance(1000);
   
    // Creates a new Company and a Thread to run its task
    Company  company=new Company(account);
    Thread companyThread=new Thread(company);
    // Creates a new Bank and a Thread to run its task
    Bank bank=new Bank(account);
    Thread bankThread=new Thread(bank);
   
View Full Code Here


     */
    Incrementer incrementer=new Incrementer(vector);
    /*
     * A decrementer task
     */
    Decrementer decrementer=new Decrementer(vector);
   
    /*
     * Create and execute 100 incrementer and 100 decrementer tasks
     */
    Thread threadIncrementer[]=new Thread[THREADS];
View Full Code Here

     */
    AtomicIntegerArray vector=new AtomicIntegerArray(1000);
    /*
     * An incrementer task
     */
    Incrementer incrementer=new Incrementer(vector);
    /*
     * A decrementer task
     */
    Decrementer decrementer=new Decrementer(vector);
   
View Full Code Here

  public static void main(String[] args) {

    /*
     * Creation of the custom executor
     */
    MyExecutor myExecutor=new MyExecutor(2, 4, 1000, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<Runnable>());
   
    /*
     * Create a list to store the objects to control the execution of the tasks
     */
    List<Future<String>> results=new ArrayList<>();
   
    /*
     * 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);
    }
   
    /*
     * Get the result of the execution of the first five tasks
     */
    for (int i=0; i<5; i++){
      try {
        String result=results.get(i).get();
        System.out.printf("Main: Result for Task %d : %s\n",i,result);
      } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
      }
    }
   
    /*
     * Call the shutdown method
     */
    myExecutor.shutdown();
   
    /*
     * Get the results of the execution of the last five tasks
     */
    for (int i=5; i<10; i++){
      try {
        String result=results.get(i).get();
        System.out.printf("Main: Result for Task %d : %s\n",i,result);
      } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
      }
    }
   
    /*
     * Wait for the finalization of the Executor
     */
    try {
      myExecutor.awaitTermination(1, TimeUnit.DAYS);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
   
    /*
 
View Full Code Here

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

    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) {

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

TOP

Related Classes of com.packtpub.java7.concurrency.chapter6.recipe04.task.Event

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.