views:

212

answers:

2

Hello fellows!

I have some code in C++ for Windows and I intend to port it to Java. But unfortunately it is not so easy as I thought. Could someone help me, please?

Please, take a look at algorithm:

HANDLE hExitEvent;
HANDLE hDataAvailabeEvent;

while(true)
{
  WaitForMultipleObjects();
  if (hExitEvent is set)
    break;
  if (hDataAvailabeEvent)
  {
    process chunk of data;
    if (all data processed)
      ResetEvent(hDataAvailabeEvent);
  }
}

hDataAvailabeEvent can be set from different threads. If all data is processed, then event resets and at calling WaitForMultipleObjects thread will suspend until new data arrives or time for thread exit comes.

I've already seen question Waitformultipleobjects in Java but it's not suitable for my situation because I can't process all new data in 1 cycle iteration and processing spreads over some iterations.

Thanks in advance!

A: 

WaitForMultipleObjects is a Windows API function. Personally I would implement this via simple events (instead of calling ResetEvent have your threads fire off some kind of event, and have one worker thread registered to listen for those events).

Max Lybbert
+2  A: 

A simple solution which would seem to fulfill your need is something like:

class DataProcessor implements Runnable {
    private final ExecutorService executor = 
        Executors.newSingleThreadedExecutor();

    public void stop { executor.shutdown(); }

    public void process(final Data data){
        executor.execute(new Runnable(){
            public void run(){
                // process data
            }
        });
    }

}

You don't really have any events, but it works more or less the same way. Want to add data to the processing queue? Call process and the executor will process your Data when it is finished with any previous tasks on the queue. Shutting down? Call stop and the executor will finish process everything on the queue, but will not accept any more work. (If you need a more abrupt end, use ExecutorService.shutdownNow).

gustafc
Thanks gustafc!I guess it is a thread pool. As a one way of the implementation it can be, but could you tell me please: will statement executor.shutdown(); finish "run" method gracefully? Can I explicitly tell objects used in "run" to shutdown in case of executor.shutdown()? Perhaps inside "run" some sort of exception will be rised?
nbulba
No, `run` will always finish gracefully. Java doesn't really do abrupt thread stops. As the docs for `shutdown` say, "submitted tasks are executed, but no new tasks will be accepted" - no new work items will be accepted, but the thread will continue running until the work queue is empty. As for `shutdownNow`, it lets the currently executing `Runnable` finish, but doesn't start working on any new items.
gustafc
If you want to be able to abort the currently executing task when your processor is shut down, you can just do something like `if (executor.isShutdown()) return;` on appropriate places in `run` .
gustafc
Come to think of it, `shutdownNow` interrupts the pool's threads, so you can use that, too (and it's probably preferable over checking `isShutdown()` if you use `shutdownNow`).
gustafc