Results 1 to 3 of 3
- 07-05-2012, 12:34 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 10
- Rep Power
- 0
Doing background task in Swing application
Hi everyone.
I have a client/server application in Swing. The clients sends a request via RMI to send an email to the server on another computer. However I want the server method to redirect directly to the client. Otherwise my Swing application will hang for some time.
I have done this with a SwingWorker thread and the doInBackground method.
I have a class variable with a queue of email messages. Everytime I call the method sendEmail, I just add another email to this queue variable. In the doInBackground method I check continuously if there are some emails in the queue and I send them and remove them afterwards from the queueu. You can see that in the above code.Java Code:emailThread = new SwingWorker<Boolean, Boolean>() { @Override protected Boolean doInBackground() throws Exception { while (!this.isCancelled()) { if (emailQueue.size() > 0) { // log("Email versturen..."); emailQueue.get(0).send(); log("Email verstuurd naar " + emailQueue.get(0).getToAddresses().get(0) + " met als onderwerp " + emailQueue.get(0).getSubject()); emailQueue.remove(0); } } return true; } };
This works fine. However there is the problem of performance. When I start the server my CPU usage at the server computer goes to 100% and slows everything down.
When I put however a Thread.sleep method in the while lus, I have the problem when I want to send 100 emails, that there is a time between every email. So that is not what I want.
How can I fix this?
- 07-05-2012, 12:56 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
Re: Doing background task in Swing application
Look into how to use wait/notify. For instance, your worker can wait on the queue, which you can notify when an item is added to the queue. Alternatively, look into the BlockingQueue implementations.
- 07-05-2012, 01:27 AM #3
Member
- Join Date
- Jun 2012
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
SwingWorker background task pause
By madroadbiker in forum Advanced JavaReplies: 5Last Post: 05-07-2011, 07:00 PM -
Swing : Background image
By Java Unknown in forum New To JavaReplies: 1Last Post: 02-24-2011, 09:52 AM -
getting running application from task manager
By gayathri_g in forum Threads and SynchronizationReplies: 3Last Post: 09-26-2009, 06:43 PM -
How to interact with Windows Task Scheduler within Java Application
By tiburblium in forum New To JavaReplies: 0Last Post: 04-12-2009, 04:16 PM -
java : how to hide application icon from a task bar
By yogeshagashe in forum Advanced JavaReplies: 0Last Post: 03-12-2008, 01:05 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks