Help with threads



Hi,

From my class I call compute to get the following done:

- create a CalcData object with values for numerator & denominator.
- Create an instance of Calculation()
- Create a new Thread with Calculation as target.
- Start the calculation.
- Then I need to wait for the thread to finnish (unsure about this part).
- Then I fire an event with fireUpdateOccurred(new UpdateEvent( this, calc.data )).


So what is the problem I have? This works only once. When I try to input new values and my compute is executed again I get a null as result. I wonder if the thread did not end gracefully?

Any hints?

cheers,

//mikael

public void compute(String numerator, String denominator){
CalcData data = new CalcData();
data.setN(numerator);
data.setD(denominator);
//My Runnable class
Calculation calc = new Calculation(data);
if (calcThread == null) {
calcThread = new Thread(calc, "Calculation");
calcThread.start();

}
// Wait for the thread to finish but don't wait longer than a
// specified time
long delayMillis = 20000; // since max is 15 seconds delay in calculation
try {
calcThread.join(delayMillis);

if (calcThread.isAlive()) {
// Timeout occurred; thread has not finished
} else {
// Finished
}
} catch (InterruptedException e) {
// Thread was interrupted
}
//wait for theard to finnish before we can fireUpdateOccurred.
fireUpdateOccurred(new UpdateEvent( this, calc.data ));


}


CalcData
========
/**
 * This class contains the data in (denominator and nominator) and out
 *  (result) for division used in Exercise2Model.
 * @author eraonel
 *
 */
public class CalcData {
	private boolean available = false;
	private String d, n,r;
	
	public CalcData() {
	}
	public String getD() {
		return d;
	}
	public void setD(String d) {
		this.d = d;
	}
	public String getN() {
		return n;
	}
	public void setN(String n) {
		this.n = n;
	}
	public String getR() {
		return r;
	}
	public void setR(String r) {
		this.r = r;
	}
	
	
	
}

Calculation
===========

import java.util.Random;



public class Calculation implements Runnable {

	
    public CalcData data;
	private static Random generator = new Random();
	
	public Calculation(CalcData data) {
		this.data = data;
    }	
	
	// Here is the actual calculation performed.
    public void run() {
    	String result;
		      // read two numbers and calculate quotient
		      try {
		    	  //pause 5-15 sec. before actual calcuation
		    	  int milliSeconds = (generator.nextInt( 10000 )+5000);
		    	  System.out.println("Pausing of seconds:"+milliSeconds);
		    	  Thread.sleep(milliSeconds);
		         Integer n = Integer.parseInt( data.getN() );
		         Integer d = Integer.parseInt( data.getD() );
		         Integer q = quotient( n, d );
		         System.out.println("Calculating");
		         result = q.toString();
		         data.setR(result);
		         System.out.println("Result is :"+result);
		
		
		      }

		      // process improperly formatted input
		      catch ( NumberFormatException numberFormatException )
		      {
		            result = "Enter two integers";
		      }

		      // process attempts to divide by zero
		      catch ( ArithmeticException arithmeticException )
		      {
					result = "Division by zero";
		      }
		      //
			  catch (InterruptedException ie){
					ie.printStackTrace();
				}
		      // process unexpected exception
				catch ( Exception exception )
				{
					result = exception.getMessage();
				}


}
//
public Integer quotient( Integer numerator, Integer denominator ) throws ArithmeticException{
return numerator / denominator;
}


}
.



Relevant Pages

  • Re: Problem to input more numbers when thread is running
    ... The problem I have is that while I am doing the pause and the actual calculation in my thread I cannot input more numbers in the gui and hence start more calculation threads. ... public void run{ ...
    (comp.lang.java.help)
  • Re: Problem to input more numbers when thread is running
    ... The problem I have is that while I am doing the pause and the actual calculation in my thread I cannot input more numbers in the gui and hence start more calculation threads. ... public void compute{ ...
    (comp.lang.java.help)
  • Re: integers and arrays in Java - how?
    ... If i try "int" in that math, the values are then zero for everything - even those where i do no calculation. ... public void mouseDragged{ ... The "do some calculation" bit is what is troubling; "int" does not work and kills reading of mouse XY coordinates. ...
    (comp.lang.java)
  • Re: integers and arrays in Java - how?
    ... If i try "int" in that math, the values are then zero for everything - even those where i do no calculation. ... public void mouseDragged{ ... The "do some calculation" bit is what is troubling; "int" does not work and kills reading of mouse XY coordinates. ...
    (comp.lang.java)