Single Thread Request Executor : Concurrent « Threads « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Threads » Concurrent 




Single Thread Request Executor
        
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

//package org.cspoker.common.util.threading;

import java.util.List;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;



public class SingleThreadRequestExecutor extends AbstractExecutorService {
  private ScheduledExecutorService executor;

  private static final SingleThreadRequestExecutor scheduledRequestExecutor = new SingleThreadRequestExecutor();

  private SingleThreadRequestExecutor() {
    executor = Executors.newSingleThreadScheduledExecutor();
  }

  public static SingleThreadRequestExecutor getInstance() {
    return scheduledRequestExecutor;
  }

  public boolean awaitTermination(long timeout, TimeUnit unit)
  throws InterruptedException {
    return executor.awaitTermination(timeout, unit);
  }

  public boolean isShutdown() {
    return executor.isShutdown();
  }

  public boolean isTerminated() {
    return executor.isTerminated();
  }

  public void shutdown() {
    executor.shutdown();
  }

  public List<Runnable> shutdownNow() {
    return executor.shutdownNow();
  }

  public void execute(final Runnable command) {
    executor.execute(new Runnable(){
      @Override
      public void run() {
        try{
          command.run();
        }catch(Throwable e){
          // This normally bad code of catch on Exception is here for a *reason*.
          // Future *eats* all exceptions *silently*. This clause at least allows
          // the exception to emit noise for debugging. This is particularly pernicious
          // if you have something like a NullPointerException
          
          e.printStackTrace();
          throw new RuntimeException(e);
        }
      }
    });
  }


  @Override
  public <T> Future<T> submit(final Callable<T> task) {
    return executor.submit(new Callable<T>(){
      @Override
      public T call() throws Exception {
        try{
          return task.call();
        }catch(Throwable e){
          // This normally bad code of catch on Exception is here for a *reason*.
          // Future *eats* all exceptions *silently*. This clause at least allows
          // the exception to emit noise for debugging. This is particularly pernicious
          // if you have something like a NullPointerException
          
          e.printStackTrace();
          throw new RuntimeException(e);
        }

      }
    });
  }

  @Override
  public Future<?> submit(final Runnable task) {
    return executor.submit(new Runnable(){
      @Override
      public void run() {
        try{
          task.run();
        }catch(Throwable e){
          // This normally bad code of catch on Exception is here for a *reason*.
          // Future *eats* all exceptions *silently*. This clause at least allows
          // the exception to emit noise for debugging. This is particularly pernicious
          // if you have something like a NullPointerException
          
          e.printStackTrace();
          throw new RuntimeException(e);
        }
      }
    });
  }

  @Override
  public <T> Future<T> submit(final Runnable task, final T result) {
    return executor.submit(new Runnable(){
      @Override
      public void run() {
        try{
          task.run();
        }catch(Throwable e){
          // This normally bad code of catch on Exception is here for a *reason*.
          // Future *eats* all exceptions *silently*. This clause at least allows
          // the exception to emit noise for debugging. This is particularly pernicious
          // if you have something like a NullPointerException
          
          e.printStackTrace();
          throw new RuntimeException(e);
        }
      }
    }, result);
  }

  public ScheduledFuture<?> schedule(final Runnable command, long delay,
      TimeUnit unit) {
    return executor.schedule(new Runnable(){
      @Override
      public void run() {
        try{
          command.run();
        }catch(Throwable e){
          // This normally bad code of catch on Exception is here for a *reason*.
          // Future *eats* all exceptions *silently*. This clause at least allows
          // the exception to emit noise for debugging. This is particularly pernicious
          // if you have something like a NullPointerException
          
          e.printStackTrace();
          throw new RuntimeException(e);
        }
      }
    }, delay, unit);
  }

  public <V> ScheduledFuture<V> schedule(final Callable<V> callable, long delay,
      TimeUnit unit) {
    return executor.schedule(new Callable<V>(){
      @Override
      public V call() throws Exception {
        try{
          return callable.call();
        }catch(Throwable e){
          // This normally bad code of catch on Exception is here for a *reason*.
          // Future *eats* all exceptions *silently*. This clause at least allows
          // the exception to emit noise for debugging. This is particularly pernicious
          // if you have something like a NullPointerException
          
          e.printStackTrace();
          throw new RuntimeException(e);
        }

      }
    }, delay, unit);
  }

  public ScheduledFuture<?> scheduleAtFixedRate(final Runnable command,
      long initialDelay, long period, TimeUnit unit) {
    return executor
    .scheduleAtFixedRate(new Runnable(){
      @Override
      public void run() {
        try{
          command.run();
        }catch(Throwable e){
          // This normally bad code of catch on Exception is here for a *reason*.
          // Future *eats* all exceptions *silently*. This clause at least allows
          // the exception to emit noise for debugging. This is particularly pernicious
          // if you have something like a NullPointerException
          
          e.printStackTrace();
          throw new RuntimeException(e);
        }
      }
    }, initialDelay, period, unit);
  }

  public ScheduledFuture<?> scheduleWithFixedDelay(final Runnable command,
      long initialDelay, long delay, TimeUnit unit) {
    return executor.scheduleWithFixedDelay(new Runnable(){
      @Override
      public void run() {
        try{
          command.run();
        }catch(Throwable e){
          // This normally bad code of catch on Exception is here for a *reason*.
          // Future *eats* all exceptions *silently*. This clause at least allows
          // the exception to emit noise for debugging. This is particularly pernicious
          // if you have something like a NullPointerException
          
          e.printStackTrace();
          throw new RuntimeException(e);
        }
      }
    }, initialDelay, delay,
    unit);
  }

}

   
    
    
    
    
    
    
    
  














Related examples in the same category
1.Demo for java.util.concurrent.Future
2.Using java.util.concurrent.locks.Lock to control the synchronized resource
3.This program shows how multiple threads can safely access a data structure
4.java.util.concurrent.ThreadPoolExecutor and thread pooljava.util.concurrent.ThreadPoolExecutor and thread pool
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.