I am coding a artifical intelligence example in Java. But I want to do some calculations about learning method and write in JTable in my form. But I am disappointed with Thread.sleep(1000)
command. Because this pauses all frame and not doing changes in real time in JTable. I want to write some code after writing table and show users changes for a second and less. How can I use Multithreading in this situation.

- 137
- 2
- 11
3 Answers
It seems like you're calling Thread.sleep(1000)
in the UI thread, which causes the UI itself to pause for 1s. You should never call that method on the UI thread. Instead you can use SwingUtilities.invokeLater()
(see invokeLater) to avoid hogging the UI thread while you're doing work.

- 8,028
- 17
- 51
- 70
Swing is a single threaded framework, this means that event processing and painting is done within the same thread. Any operation (like Thread.sleep
) which blocks this thread, will prevent it from processing new events or repainting the screen.
Swing is also not thread safe, so updates to the UI MUST be made from within the context of the Event Dispatching Thread...
Instead, you could use:
- A Swing
Timer
, set to repeat every 1 second, which, when triggered, will execute it's event notification within the context of the Event Dispatching Thread - A
SwingWorker
, which would allow you to useThread.sleep
within it'sdoInBackground
method, but providespublish
/process
methods which can be used to sync updates back to the Event Dispatching Thread safety - A
Thread
andSwingUtilities.invokeLater
, which makes the entire process of ensuring that updates are synchronized with the EDT correctly up to you.
See:
for more details
For example:

- 1
- 1

- 343,457
- 22
- 230
- 366
Have the class that implements the DefaultTableModel query an object for new updates on a scheduler. If the object that listens for all updates is shared, you'll be able to see everyone's changes.
One good way to do this would be a web service. Even better? A push model from central listener to all remote DefaultTableModel listeners whenever a new event arrives.
You shouldn't be using Thread
anymore. Since JDK 6, the preferred way to do this would be ExecutorService
.

- 305,152
- 44
- 369
- 561