FileDocCategorySizeDatePackage
ThreadRace.javaAPI DocExample782Thu Feb 24 15:23:06 GMT 2000tuning.threads

ThreadRace

public class ThreadRace extends Object implements Runnable

Fields Summary
static int
num
Constructors Summary
Methods Summary
public static voidincrement()


     
  
    int n = num;
    //This next line gives the context switcher an ideal
    //place to switch context.
    System.out.print(num+" ");
    //And when it switches back, n will still be the old
    //value from the old thread.
    num = n + 1;
  
public static voidmain(java.lang.String[] args)

    ThreadRace d1 = new ThreadRace();
    ThreadRace d2 = new ThreadRace();

    Thread d1Thread = new Thread(d1);
    Thread d2Thread = new Thread(d2);

    d1Thread.start();
    d2Thread.start();
  
public voidrun()

    for (int i = 200; i >= 0 ; i--)
    {
      increment();
    }