Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2005

Tip: Looking for answers? Try searching our database.

Problem to input more numbers when thread is running

Thread view: 
Petterson Mikael - 08 Dec 2005 18:41 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.