Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Threads and Synchronization and the fly likes Help with implementation - should I use threads??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Help with implementation - should I use threads???" Watch "Help with implementation - should I use threads???" New topic
Author

Help with implementation - should I use threads???

Pedro Cristo
Greenhorn

Joined: Sep 23, 2012
Posts: 2
Hi!

I have recently (2 months ago) started to learn java altought I have already experience of another languages. One of the things that I don't have experience in, is threads and I'm trying to develop an application and I think the problem is related with threads. Let me explain.

I'm developing an application to work on lottery statistics with the possibility to filter the combinations result using filters.
So, the user picks numbers from a range and my software will get each combination possible, check it against the filter(s) condition(s) and add the combination to a list or a file.

At the moment, I have all this implemented (with just one filter...) but the problem is that the GUI hangs because I'm not using threads. The process is very quick at the moment, even with many more filters. The problem is that I don't want the GUI to hang up.

So, my question is: how can I implement this with threads (or any other solution that doesn't hang up the GUI)??? Where do I implement the thread or threads? In the MainWindow class? In the Filters class?? On both?

Those are my doubts about Threads in java.

Here is my code:





Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2250
    
  47
but the problem is that the GUI hangs because I'm not using threads

All applications use threads whether you explicitly create them or not. The GUI hangs because you are doing all the computation on the Event Dispatch Thread which is the only thread that interacts with Swing components.
I don't have time to go through all your code to see exactly what you are doing but if you have a long running task that updates the GUI then the solution is generally to use a javax.swing.SwingWorker.
Pedro Cristo
Greenhorn

Joined: Sep 23, 2012
Posts: 2
Tony Docherty wrote:
but the problem is that the GUI hangs because I'm not using threads

All applications use threads whether you explicitly create them or not. The GUI hangs because you are doing all the computation on the Event Dispatch Thread which is the only thread that interacts with Swing components.
I don't have time to go through all your code to see exactly what you are doing but if you have a long running task that updates the GUI then the solution is generally to use a javax.swing.SwingWorker.


Thanks Tony, i will have a look at that.
When I said that I wasn't using threads I meant that I didn't created a new one to run my code, so you're right.
Anyway, I was trying to implement it and I think I've reached a good solution. I will post my code here when I have time.

Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help with implementation - should I use threads???