Package thread.concurrencyCookbook.chapter6.recipe02

Examples of thread.concurrencyCookbook.chapter6.recipe02.Client


    // Generate a document with 100 lines and 1000 words per line
    DocumentMock mock=new DocumentMock();
    String[][] document=mock.generateDocument(100, 1000, "the");
 
    // Create a DocumentTask
    DocumentTask task=new DocumentTask(document, 0, 100, "the");
   
    // Create a ForkJoinPool
    ForkJoinPool pool=new ForkJoinPool();
   
    // Execute the Task
    pool.execute(task);
   
    // Write statistics about the pool
    do {
      System.out.printf("******************************************\n");
      System.out.printf("Main: Parallelism: %d\n",pool.getParallelism());
      System.out.printf("Main: Active Threads: %d\n",pool.getActiveThreadCount());
      System.out.printf("Main: Task Count: %d\n",pool.getQueuedTaskCount());
      System.out.printf("Main: Steal Count: %d\n",pool.getStealCount());
      System.out.printf("******************************************\n");

      try {
        TimeUnit.SECONDS.sleep(1);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
     
    } while (!task.isDone());

    // Shutdown the pool
    pool.shutdown();
   
    // Wait for the finalization of the tasks
    try {
      pool.awaitTermination(1, TimeUnit.DAYS);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
   
    // Write the results of the tasks
    try {
      System.out.printf("Main: The word appears %d in the document",task.get());
    } catch (InterruptedException | ExecutionException e) {
      e.printStackTrace();
    }
  }
View Full Code Here


   * Main method of the class
   */
  public static void main(String[] args) {
   
    // Generate a document with 100 lines and 1000 words per line
    DocumentMock mock=new DocumentMock();
    String[][] document=mock.generateDocument(100, 1000, "the");
 
    // Create a DocumentTask
    DocumentTask task=new DocumentTask(document, 0, 100, "the");
   
    // Create a ForkJoinPool
View Full Code Here

  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

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

   */
  public static void main(String[] args) throws Exception {
    /*
     * Create a Lock object
     */
    MyLock lock=new MyLock();
   
    /*
     * Create an array for five threads
     */
    Thread threads[]=new Thread[5];
   
    /*
     * 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();
    }
   
    /*
     * Create a loop with 15 steps
     */
    for (int i=0; i<15; i++) {
      /*
       * Write info about the lock
       */
      System.out.printf("Main: Logging the Lock\n");
      System.out.printf("************************\n");
      System.out.printf("Lock: Owner : %s\n",lock.getOwnerName());
      System.out.printf("Lock: Queued Threads: %s\n",lock.hasQueuedThreads());
      if (lock.hasQueuedThreads()){
        System.out.printf("Lock: Queue Length: %d\n",lock.getQueueLength());
        System.out.printf("Lock: Queued Threads: ");
        Collection<Thread> lockedThreads=lock.getThreads();
        for (Thread lockedThread : lockedThreads) {
        System.out.printf("%s ",lockedThread.getName());
        }
        System.out.printf("\n");
      }
      System.out.printf("Lock: Fairness: %s\n",lock.isFair());
      System.out.printf("Lock: Locked: %s\n",lock.isLocked());
      System.out.printf("************************\n");
      /*
       * Sleep the thread for one second
       */
      TimeUnit.SECONDS.sleep(1);
 
View Full Code Here

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

Related Classes of thread.concurrencyCookbook.chapter6.recipe02.Client

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.