T
T
trace82016-09-12 18:57:38
Java
trace8, 2016-09-12 18:57:38

How to pause a loop in Java?

The task such
There is a form in which there are some fields. They enter numbers for calculations. When the data is entered, the button is pressed and the loop starts with several iterations. Inside the cycle, calculations are made based on these data. The problem is that at each iteration you need to correct the data in the form. It turns out such a dialogue is constant with the user.
Pushing thrread.sleep() to the beginning of each iteration is not an option because you can't enter new data that way. What is deeeelaaaat?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fat Lorrie, 2016-09-12
@Free_ze

In a good way, heavy logic (and sleep calls can be called a heavy task, because it slows down a thread that cannot process interface events) needs to be moved to a separate thread (thread), GUI thread should behave passively and deal only with interaction with user and background worker launches (for lengthy operations).
In your case, there is a simple way: divide tasks into iterations and execute them on a timer.
Or organize a full-fledged background thread that can be unrestrictedly slowed down through sleep.
If high school English allows, I advise you to go through this tutorial .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question