package cputest;

public class TestThread extends Thread{

  private boolean stop=false;
  private String name="Thread-";
  private java.io.PrintStream out=null;
  private java.io.Writer writer=null;


  public TestThread(int i,java.io.PrintStream out) {
    this.name=this.name+i;
    this.out=out;
  }

  public TestThread(int i,java.io.Writer writer) {
    this.name=this.name+i;
    this.writer=writer;
  }

  public void run() {
    java.util.Random random=new java.util.Random(234234234);
    int i=1;
    while(!stop) {
      i++;
      double d=(random.nextDouble()*random.nextDouble())/((random.nextDouble()*random.nextDouble())+1);
      double e=(random.nextDouble()*random.nextDouble())/((random.nextDouble()*random.nextDouble())+1);
      d=d-e;
      if (i%1000000==0) {
        if (out!=null)
          out.println(this.name + "--> " + i + " iterations");
        else
          try {
            writer.write(this.name + "--> " + i + " iterations\n");
          }catch(Exception err){}
      }
    }
  }

  public void stopThread(){
    this.stop=true;
  }

  public static void main(String [] args) {
    int cores=Integer.parseInt(args[0]);
    int time=Integer.parseInt(args[1]);
    java.util.Vector v=new java.util.Vector();
    System.out.println("***********START*******************");
    for(int i=1;i<=cores;i++) {
      TestThread t=new TestThread(i,System.out);
      t.start();
      v.addElement(t);
    }
    try {
      Thread.sleep(time * 1000);
    }catch(Exception err) {}
    for(int i=0;i<v.size();i++) {
      TestThread t=(TestThread)v.elementAt(i);
      t.stopThread();
    }
    System.out.println("************STOP*******************");
  }


}
