Re: Problem to input more numbers when thread is running
- From: Petterson Mikael <mikael.petterson@xxxxxxxxxxxxxxx>
- Date: Thu, 08 Dec 2005 19:49:35 +0100
Petterson Mikael wrote:
Hi,
When I have input two numbers to calculate from my gui. Then I use compute(). Within the compute I start a thread that performs the actual computation. The problem I have is that while I am doing the pause ( on purpose) and the actual calculation in my thread I cannot input more numbers in the gui and hence start more calculation threads.
Any ideas why?
cheers,
//mikael
Ps. If you need more code to read just let me know.
//compute()
===========
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);
//Create a thread within the parent group.
Thread calcThread = new Thread(tg,calc);
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));
}
My thread =========
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;
}
}
Just a thought:
Could it be something with the CalcData object that it is not synchronized?
cheers,
//mikael .
- Follow-Ups:
- Re: Problem to input more numbers when thread is running
- From: Knute Johnson
- Re: Problem to input more numbers when thread is running
- References:
- Problem to input more numbers when thread is running
- From: Petterson Mikael
- Problem to input more numbers when thread is running
- Prev by Date: Re: loops, what the heck?!?
- Next by Date: Re: Xeon processor and Java - better multithreading?
- Previous by thread: Problem to input more numbers when thread is running
- Next by thread: Re: Problem to input more numbers when thread is running
- Index(es):
Relevant Pages
|
|