Help with threads
- From: Petterson Mikael <mikael.petterson@xxxxxxxxxxxxxxx>
- Date: Mon, 28 Nov 2005 15:13:18 +0100
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;
}
} .
- Prev by Date: Re: cant's compile with javac
- Next by Date: Re: cant's compile with javac
- Previous by thread: Newbie question on stopping an application
- Next by thread: Re: Help with threads
- Index(es):
Relevant Pages
|