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;
}
}
Petterson Mikael - 08 Dec 2005 18:49 GMT
> Hi,
>
[quoted text clipped - 108 lines]
>
> }
Just a thought:
Could it be something with the CalcData object that it is not synchronized?
cheers,
//mikael
Knute Johnson - 08 Dec 2005 23:15 GMT
>> Hi,
>>
[quoted text clipped - 108 lines]
>
> //mikael
It's because you are doing your calculations on the EDT. When you join
the calc thread the EDT stops and so does your ability to input more data.

Signature
Knute Johnson
email s/nospam/knute/
Mark Haase - 09 Dec 2005 00:35 GMT
> Hi,
>
[quoted text clipped - 5 lines]
>
> Any ideas why?
Because your first thread pauses while the 2nd thread computes its
answer. If the first thread is the EDT (event dispatching thread) then
while its blocked you can't update the GUI or receive events. You'll
know if the first thread is the EDT because you'll be calling compute()
from some GUI event handler, like actionPerformed(..)
You've got the right idea, but wherever you want to use the result, have
the calculation thread perform that action when its complete, and let
the EDT return. There's no need to use join(), instead let your
calculation thread return from whatever method you've called on it.
|\/| /| |2 |<
mehaase(at)gmail(dot)com
blmblm@myrealbox.com - 09 Dec 2005 11:28 GMT
>Hi,
>
[quoted text clipped - 5 lines]
>
>Any ideas why?
Others have answered this question.
[ snip ]
> //Create a thread within the parent group.
> Thread calcThread = new Thread(tg,calc);
[quoted text clipped - 5 lines]
> try {
> calcThread.join(delayMillis);
What you seem to be doing here is starting a thread and then waiting
for it to complete. Usually the point of doing something (calculations
here) in a separate thread is to make it possible for the original
thread to do something else while the new thread is doing its thing.
But you don't allow that here -- the original thread waits for the
new thread to complete its work. So why have a separate thread?
[ snip ]
| B. L. Massingill
| ObDisclaimer: I don't speak for my employers; they return the favor.