Package com.packtpub.java7.concurrency.chapter2.recipe4.task

Examples of com.packtpub.java7.concurrency.chapter2.recipe4.task.Reader


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

    // Create a ConcurrentLinkedDeque to work with it in the example
    LinkedBlockingDeque<String> list=new LinkedBlockingDeque<>(3);
   
    Client client=new Client(list);
    Thread thread=new Thread(client);
    thread.start();
   
    for (int i=0; i<5 ; i++) {
      for (int j=0; j<3; j++) {
View Full Code Here


   
    /*
     * Launch 100 Consumer tasks
     */
    for (int i=0; i<THREADS; i++){
      Consumer consumer=new Consumer("Consumer "+i,buffer);
      consumerThreads[i]=new Thread(consumer);
      consumerThreads[i].start();
    }
   
    /*
 
View Full Code Here

   
    /*
     * Launch 100 Producer tasks
     */
    for (int i=0; i<THREADS; i++) {
      Producer producer=new Producer("Producer: "+i,buffer);
      producerThreads[i]=new Thread(producer);
      producerThreads[i].start();
    }
   
    /*
 
View Full Code Here

    /*
     * Write the events in the console
     */
    System.out.printf("Main: Queue Size: %d\n",queue.size());
    for (int i=0; i<taskThreads.length*1000; i++){
      Event event=queue.poll();
      System.out.printf("Thread %s: Priority %d\n",event.getThread(),event.getPriority());
    }
    System.out.printf("Main: Queue Size: %d\n",queue.size());
    System.out.printf("Main: End of the program\n");
  }
View Full Code Here

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

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

   
    /*
     * Write the first element of the map
     */
    Map.Entry<String, Contact> element;
    Contact contact;
   
    element=map.firstEntry();
    contact=element.getValue();
    System.out.printf("Main: First Entry: %s: %s\n",contact.getName(),contact.getPhone());
   
    /*
     * Write the last element of the map
     */
    element=map.lastEntry();
    contact=element.getValue();
    System.out.printf("Main: Last Entry: %s: %s\n",contact.getName(),contact.getPhone());

    /*
     * Write a subset of the map
     */
    System.out.printf("Main: Submap from A1996 to B1002: \n");
    ConcurrentNavigableMap<String, Contact> submap=map.subMap("A1996", "B1002");
    do {
      element=submap.pollFirstEntry();
      if (element!=null) {
        contact=element.getValue();
        System.out.printf("%s: %s\n",contact.getName(),contact.getPhone());
      }
    } while (element!=null);
  }
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

TOP

Related Classes of com.packtpub.java7.concurrency.chapter2.recipe4.task.Reader

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.