FileDocCategorySizeDatePackage
Worker.javaAPI DocExample3262Thu May 23 09:32:50 BST 2002 sample.standard

Worker

public abstract class Worker extends Basic implements Runnable, WorkerMBean
Base class for the workers in the sample application.

Fields Summary
public static final String
OBJECT_NAME
protected Queue
_queue
protected long
_totalProcessingTime
protected boolean
_stopCalled
protected int
_workFactor
protected long
_numberOfUnitsProcessed
protected boolean
_suspended
Constructors Summary
public Worker(Queue queue, int workFactor)


          
        _queue = queue;
        _workFactor = workFactor;
    
Methods Summary
voidcalculatePrimes(int numberOfPrimesToCalculate)
In this method is where the "work" takes place. We need a way to simulate the application doing something, so we calculate the first workFactor primes, starting with zero. So, the work factor is equal to the number of primes that are calculated.

        // There is probably an easier way to do this,
        /// but it seemed a good way to chew up some CPU
        long startTime = System.currentTimeMillis();
        long number = 1;
        int numberOfPrimesCalculated = 0;
        while (numberOfPrimesCalculated < numberOfPrimesToCalculate) {
            long currentNumber = 1;             // start with 1
            long numberOfFactors = 0;
            while (currentNumber <= number) {
                if ((number%currentNumber) == 0) {
                    // This is *definitely* the long way around, but
                    /// remember, we *want* to eat up lots of CPU...
                    numberOfFactors++;
                }
                currentNumber++;
            }
            // The number is prime if it only has two factors
            /// (i.e., itself and 1)
            if (numberOfFactors == 2) {
                numberOfPrimesCalculated++;
                //                System.out.println("Supplier.calculatePrimes(): INFO: " +
                //                    "Prime number found - " + number);
            }
            number++;
        }
        _totalProcessingTime += (System.currentTimeMillis() - startTime);
    
public floatgetAverageUnitProcessingTime()

        return  (_numberOfUnitsProcessed > 0) ? (float)_totalProcessingTime/(float)_numberOfUnitsProcessed :
                0.0f;
    
public longgetNumberOfUnitsProcessed()

        return  _numberOfUnitsProcessed;
    
public intgetWorkFactor()

        return  _workFactor;
    
public booleanisSuspended()

        return  _suspended;
    
public voidreset()

        _numberOfUnitsProcessed = 0;
        _totalProcessingTime = 0;
    
public synchronized voidresume()

        _suspended = false;
        notifyAll();
    
public abstract voidrun()

public voidsetNumberOfUnitsProcessed(long value)

        _numberOfUnitsProcessed = value;
    
public voidsetWorkFactor(int workFactor)

        _workFactor = workFactor;
    
public synchronized voidstop()

        _stopCalled = true;
    
public synchronized voidsuspend()

        _suspended = true;