Implementing an operations queue involves a combination of two threading classes:
Together, these two classes create an operations queue for handling
user actions in the file manager more effectively by executing them
in a separate thread. Many methods in the FileSelector
use the operations queue.
The Operation
class defines an interface for
operations performed in the FileSelector
command
queue:
// Defines the interface for a single operation executed by the commands queue interface Operation { // Implement here the operation to be executed void execute(); }
The OperationsQueue
class uses the Operation
interface to execute operations serially in a separate thread. The
operations are executed in the run
method, which
creates a Vector
for a queue of operations and executes
them as they are assigned.
To implement the OperationsQueue
class:
Create the OperationsQueue.java
class file.
Import the required classes.
import java.util.*;
Set OperationsQueue
to implement Runnable
,
and create the required variables.
// Simple Operations Queue // It runs in an independent thread and executes Operations serially class OperationsQueue implements Runnable { private volatile boolean running = true; private final Vector operations = new Vector();
Create a new Thread
and a queuing method for the upcoming operations.
OperationsQueue() { // Notice that all operations will be done in another // thread to avoid deadlocks with GUI thread new Thread(this).start(); } void enqueueOperation(Operation nextOperation) { operations.addElement(nextOperation); synchronized (this) { notify(); } }
Create a method
for stopping the Thread
and a while-loop for handling
the queued operations.
// stop the thread void abort() { running = false; synchronized (this) { notify(); } } public void run() { while (running) { while (operations.size() > 0) { try { // execute the first operation on the queue ((Operation) operations.firstElement()).execute(); } catch (Exception e) { // Nothing to do. It is expected that each operations handle // their own locally exception but this block is to ensure // that the queue continues to operate } operations.removeElementAt(0); } synchronized (this) { try { wait(); } catch (InterruptedException e) { // it doesn't matter } } } } }
Now that you have implemented the operations queue, implement the file browser functionality.