new threading structure.

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

new threading structure.

Peter Veentjer - Anchor Men

I have been playing with a new threading structure and would like to
know what other (with more concurrency experience) developers think
about it.

I have called the structure: Repeater. A Repeaters keeps repeating a
single task and one of the concrete implementations is the
ThreadPoolRepeater. The ThreadPoolRepeater has a pool of threads that
keep repeating a single task (it can be compared to a
ThreadPoolExecutor). If the task is set to null, all threads go to sleep
untill a new task is available. The task is a plain old runnable.

I have used this threading component in a few projects and find it very
usefull. It allows me to remote the thread creating aspect of components
and now I only have to make them threadsafe.

Example of the repeater:
It could be used to deal with webrequests.

class ProcessWebrequestTask implements Runnable{
   void run(){
       WebRequest request = requestQueue.take();
       process(request);
   }
}

BlockingQueue<WebRequest> requestQueue = new LinkedBlockingQueue();
//we want to deal with 50 concurrent users.
RepeaterService repeater = new ThreadPoolRepeater(new
ProcessWebrequestTask(),50);
repeater.start();

The repeater functionality isn`t very easy to implement with an executor
and that is why I have created a new threading component. What do you
think about it?


Met vriendelijke groet,

Peter Veentjer
Anchor Men Interactive Solutions - duidelijk in zakelijke
internetoplossingen

Praediniussingel 41
9711 AE Groningen

T: 050-3115222
F: 050-5891696
E: [hidden email]
I : www.anchormen.nl

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

RE: new threading structure.

Peter Veentjer - Anchor Men
This is another example. The first is the code that is in the javadoc of
the ExecutorService.

 class NetworkService {
    private final ServerSocket serverSocket;
    private final ExecutorService pool;

    public NetworkService(int port, int poolSize) throws IOException {
      serverSocket = new ServerSocket(port);
      pool = Executors.newFixedThreadPool(poolSize);
    }
 
    public void serve() {
      try {
        for (;;) {
          pool.execute(new Handler(serverSocket.accept()));
        }
      } catch (IOException ex) {
        pool.shutdown();
      }
    }
  }

  class Handler implements Runnable {
    private final Socket socket;
    Handler(Socket socket) { this.socket = socket; }
    public void run() {
      // read and service request
    }
 }


This is the networkservice rewritten with a repeater.

 class NetworkService {
    private final ServerSocket serverSocket;
    private final RepeaterService repeater;

        public NetworkService(int port, int poolSize) throws IOException
{
                serverSocket = new ServerSocket(port);
                repeater = new ThreadPoolRepeater(poolsize);      
        }
 
    public void serve() {
    repeater.setTask(new Handler());
    repeater.start();
    }

  class Handler implements Runnable {    
    public void run() {
    try{
    Socket socket = serverSocket.accept();
    // read and service request
    }catch(IOException ex){
    repeater.shutdown();
    }    
    }
    }
 }

The difference is that the serve method doesn`t block anymore. The
Repeater provides all the threads.

-----Oorspronkelijk bericht-----
Van: [hidden email]
[mailto:[hidden email]] Namens Peter
Veentjer - Anchor Men
Verzonden: donderdag 8 december 2005 10:32
Aan: [hidden email]
Onderwerp: [concurrency-interest] new threading structure.


I have been playing with a new threading structure and would like to
know what other (with more concurrency experience) developers think
about it.

I have called the structure: Repeater. A Repeaters keeps repeating a
single task and one of the concrete implementations is the
ThreadPoolRepeater. The ThreadPoolRepeater has a pool of threads that
keep repeating a single task (it can be compared to a
ThreadPoolExecutor). If the task is set to null, all threads go to sleep
untill a new task is available. The task is a plain old runnable.

I have used this threading component in a few projects and find it very
usefull. It allows me to remote the thread creating aspect of components
and now I only have to make them threadsafe.

Example of the repeater:
It could be used to deal with webrequests.

class ProcessWebrequestTask implements Runnable{
   void run(){
       WebRequest request = requestQueue.take();
       process(request);
   }
}

BlockingQueue<WebRequest> requestQueue = new LinkedBlockingQueue(); //we
want to deal with 50 concurrent users.
RepeaterService repeater = new ThreadPoolRepeater(new
ProcessWebrequestTask(),50); repeater.start();

The repeater functionality isn`t very easy to implement with an executor
and that is why I have created a new threading component. What do you
think about it?


Met vriendelijke groet,

Peter Veentjer
Anchor Men Interactive Solutions - duidelijk in zakelijke
internetoplossingen

Praediniussingel 41
9711 AE Groningen

T: 050-3115222
F: 050-5891696
E: [hidden email]
I : www.anchormen.nl

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest