FileDocCategorySizeDatePackage
ExplicitLocks2.javaAPI DocExample1693Thu Feb 24 22:14:38 GMT 2000tuning.threads

ExplicitLocks2

public class ExplicitLocks2 extends Object implements Runnable

Fields Summary
static int
REPEAT
static int
WORK
private static long
startTime
static int
blueBoxes
static int
redBoxes
private static Object
BLUE_LOCK
private static Object
RED_LOCK
Constructors Summary
Methods Summary
public static voidintensiveWork()

    //simulate some intensive work
    for(int i = WORK; i > 0; i--);
  
public static voidmain(java.lang.String[] args)


      
  
    REPEAT = (args.length > 0) ? Integer.parseInt(args[0]) : 10;
    WORK = (args.length > 1) ? Integer.parseInt(args[1]) : 1000000;
    (new Thread(new ExplicitLocks2())).start();
    (new Thread(new ExplicitLocks2())).start();
    (new Thread(new ExplicitLocks2())).start();
    (new Thread(new ExplicitLocks2())).start();
    (new Thread(new ExplicitLocks2())).start();
    (new Thread(new ExplicitLocks2())).start();
    (new Thread(new ExplicitLocks2())).start();
    (new Thread(new ExplicitLocks2())).start();
    (new Thread(new ExplicitLocks2())).start();
  
public voidrun()

    if (startTime == -1)
      startTime = System.currentTimeMillis();
    for (int i = -REPEAT; i < REPEAT; i++)
    {
        updateBlueBox(i);
        updateRedBox(i);
    }
    System.out.println("Time taken: " + (System.currentTimeMillis()-startTime));
  
public static voidupdateBlueBox(int addition)

      
  
    synchronized (BLUE_LOCK) {
      blueBoxes += addition;
    }
    intensiveWork();
  
public static voidupdateRedBox(int addition)

    synchronized (RED_LOCK) {
      redBoxes += addition;
    }
    intensiveWork();