简体   繁体   中英

Best way to retrieve mssql data without affecting the app in java swing

Hi guys im developing a chat program using java swing and mssql 2005 for which im getting data from more than 5 tables..

to get that done i used Timer in java so that it checks for new message every 2 second.. But the problem is it affects the performance and smoothness of my app..

So i tried using Thread which enhanced some performance.. is there any other way to do this so that checking for new messages and user availability will be done on one part and app runs smoothly on other part...?

A Timer is the right thing to use to execute periodic tasks in the background. Under the hood it is using a separate thread to run the tasks, so you shouldn't need to mess with threads yourself. Slowness of your Swing application is usually due to long running tasks on the Event Dispatch Thread (EDT). It's likely your timer task is putting a Runnable on the EDT once you've got the data to update the UI. Check what that does and ensure its only updating the UI and not doing other work that could be done in the background task. Check the efficiency of how you're updating the UI.

See the Java documentation on Threads and Swing and this article on Improve Application Performance With SwingWorker (this last one talks about SwingWorker not Timer, but the concepts of taking work out of the EDT are the same).

If you post some code we may be able to pinpoint the issue more closely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM