#Java thread problem

38 messages · Page 1 of 1 (latest)

dusk idol
#

hi, i've got some problems regarding the thread of my project, as you can see in the picture i'm trying to show all the steps towards reaching the wanted number in a binary search but it skips to the final part even though i've put 5 seconds at each step. can somebody help me solve this issue, please?

                colorLabel(Integer.toString(arrayLabel[0]), Color.BLUE);
                colorLabel(Integer.toString(arrayLabel[arrayLabel.length-1]), Color.BLUE);
                BinarySearchAlgorithm(arrayLabel, 0, arrayLabel.length-1, 1);
}

public void BinarySearchAlgorithm(int arr[], int l, int r, int x) {
        int auxl=l, auxr=r, auxm;
        while (l <= r) {
            int m = l + (r - l) / 2;
            colorLabel(Integer.toString(arrayLabel[m]), Color.BLUE);
            if (arr[m] == x) {
                colorLabel(Integer.toString(arrayLabel[m]), Color.GREEN);
                return;
            }
            try {
                Thread.sleep(5000);
            } catch (InterruptedException ex) {
                System.out.println(ex.toString());
            }
            if (arr[m] < x) {
                l = m + 1;
                colorLabel(Integer.toString(arrayLabel[auxl]), Color.BLACK);
                colorLabel(Integer.toString(arrayLabel[l]), Color.BLUE);
            }
            else {
                r = m - 1;
                colorLabel(Integer.toString(arrayLabel[auxr]), Color.BLACK);
                colorLabel(Integer.toString(arrayLabel[r]), Color.BLUE);
            }
            colorLabel(Integer.toString(arrayLabel[m]), Color.BLACK);
        }
    }```
tiny falconBOT
#

This post has been reserved for your question.

Hey @dusk idol! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

slate forge
#

are you using swing to visualize the algorithm ?

dusk idol
#

yes

#

those are labels from swing library

#

i'm using thread.sleep, thinking it will stop for 5 seconds but turns out the main thread ignores the ui

slate forge
#

it doesn't and you are not on the main thread. Your ui is probably not responsive during the time the algorithm runs, right ?

dusk idol
#

yes, it becomes responsive right at the end

slate forge
#

yeah you are executing your algorithm on the EDT that's the Event dispatch thread from swing itself. When you are blocking that with your sleep() calls your entire ui is rendered unresponsive and will only update after the algorithm completed.

dusk idol
#

that sounds familiar

#

should i move it to another thread and make it running?

slate forge
#

yeah you have two options. Either use a java.util.Timer or a SwingWorker. Try the apporach using the timer first. You would simply wrap the method call to BinarySearchAlgorithm with a new timer object and execute that object.

#

nevermind you have to create a Timer first and then schedule a TimerTask to be executed by that Timer, my bad

dusk idol
#

so, i create a class SwingWorker and catch the BinarySearchAlgorithm within an interval of seconds?

#

oh wait

#

so, does this method work like a task?

slate forge
#

yeah just schedule a new task that simply runs this method.

dusk idol
#

and where should i place schedule and start methods?

slate forge
#

so you would create the Timer object that executes the tasks somewhere in the constructor and schedule the actual task at the point where you are currently executing your method.

dusk idol
#

a Timer or a TimerTask object?

slate forge
#

the Timer in the constructor, the TImerTask object in your actionPerformed method.

dusk idol
#

like, timer.schedule(new TimerTask() {BinarySearchAlgorithm(...)}, 5000) ?

slate forge
#
private Timer timer = new java.util.Timer();
public Constructor() {
   timer = new Timer();
}

....
//Where you previously called BinarySearchAlgorithm
timer.schedule(new TimerTask() {
  public void run() {
    BinarySearchAlgorithm(arrayLabel, 0, arrayLabel.length-1, 1);
  }
},0);
#

yeah but I would set the initial delay to 0

dusk idol
#

will this keep 5 seconds every time in the while loop?

slate forge
#

since you are using Thread.sleep in your while loop, yes that will execute one step of the algorithm every 5 seconds.

dusk idol
#

let me implement this quickly

#

i think that's it

#

let me try more examples

#

this it, thank you so much

tiny falconBOT
# dusk idol this it, thank you so much

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

dusk idol
#

have a nice day, i'll close this one

slate forge
#

you're welcome. You might want to refine your current approach if you want to work with dynamic pauses, meaning the user can decide how long the program should pause between each iteration.

dusk idol
#

could this be just an input from the user using an integer and place it instead of 5000 milliseconds?

slate forge
#

yeah or a slider on the screen.

dusk idol
#

i'll try to get more familiar with tasks, threading and i ll probably implement it

#

see you