Multithreaded Processing
Fast multithreaded processing
Creating a multithreaded executor, you only really need three things to begin processing data with multiple threads.
- A Data type for the threaded queue (This example uses strings)
- Queue the data
- Actively dispatch threads by invoking dispatch();
// Set up a queue of strings with a max of 4 threads
MultithreadedQueueExecutor<String> queue = new MultithreadedQueueExecutor<String>(4)
{
@Override
public void onProcess(String t)
{
// This is called async when something is processed
t.toLowerCase().toUpperCase().toLowerCase().toCharArray();
}
};
// Queue stuff
for(int i = 0; i < 1024; i++)
{
queue.queue(UUID.randomUUID().toString());
}
// Tick it 20 times a second (or less?)
new Task(0)
{
@Override
public void run()
{
//We wont be constantly piling on the queue
//Might aswell stop ticking it when finished
if(queue.getQueue().isEmpty())
{
cancel();
return;
}
// Dispatch new threads if there are any
queue.dispatch();
}
};