Re: Problem to input more numbers when thread is running



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
.



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)
  • 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. ...
    (comp.lang.java.help)
  • Re: How to kill a thread?
    ... politely ask it to die, ... raising a 'ThreadAbort' exception in the target thread. ... release all resources as needed - but what happens if the thread abort ... (performing a long running background calculation for example) ...
    (comp.lang.python)
  • Re: Good practice for returning "status" from functions
    ... You expect an exception to occur in this ... Here I must pass in the entire policy, loop over each item with coverage, ... Should a calculation fail, or we can determine bad values, we pass back ...
    (microsoft.public.dotnet.general)
  • Re: What c.l.pys opinions about Soft Exception?
    ... Exception can be added to the language silentlyso that new ... programmers doesn't _need_ to know about it (although knowing it could ... Soft Exception is an exception that if is unhandled, ... clumping be done on force calculation level, ...
    (comp.lang.python)