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

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


    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

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

    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);
   
View Full Code Here

   
    /*
     * 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();
View Full Code Here

   */
  public static void main(String[] args) throws Exception {
    /*
     * Create a task object
     */
    Task task = new Task();
   
    /*
     * Create an array to store the threads
     */
    Thread threads[] = new Thread[5];
View Full Code Here

    /*
     * Create and launch a thread every 200 milliseconds. After each creation,
     * show information about the semaphore
     */
    for (int i=0; i<threads.length; i++) {
      Task task=new Task(semaphore);
      threads[i]=new Thread(task);
      threads[i].start();
     
      TimeUnit.MILLISECONDS.sleep(200);
     
View Full Code Here

    /*
     * Create and submit ten tasks
     */
    Random random=new Random();
    for (int i=0; i<10; i++) {
      Task task=new Task(random.nextInt(10000));
      executor.submit(task);
    }
   
    /*
     * Write information about the executor
View Full Code Here

TOP

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

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.