Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CountDownLatch.await() problem? #17

Open
Juanjo3299 opened this issue Dec 4, 2022 · 7 comments
Open

CountDownLatch.await() problem? #17

Juanjo3299 opened this issue Dec 4, 2022 · 7 comments

Comments

@Juanjo3299
Copy link

Juanjo3299 commented Dec 4, 2022

Hi everyone, in the demo/basic functions application sometimes when importing the same song twice, at the moment when "latchCountdown.await();" is called. It doesn't go to the next line of code, this affects the fact that I don't know exactly when that song finished loading.

@xuxiapu
Copy link
Contributor

xuxiapu commented Dec 5, 2022

@Juanjo3299 Dear developer,

1、“latchCountdown.await();” It is waiting to generate waveform data buffer. For obtaining waveform data, you can refer to this link: https://developer.huawei.com/consumer/en/doc/development/Media-Guides/basic_functions-0000001224604517#section445719453269
2. In addition, what is your usage scenario? If the same audio needs to be imported multiple times, it is recommended that you lock it when obtaining waveform data.

@Juanjo3299
Copy link
Author

The scenario is this, in the first or twice time that I import the same audio everything happens correctly, however in to the third time the countdown does not release the thread when the wave calculation is finished, right now I put a time limit of 20 seconds and you will notice that the countdown still has a size of 1 element. Could you give me some suggestions on how to solve it or why it could be giving this error? Thank you
Captura de pantalla 2022-12-05 221337
Captura de pantalla 2022-12-05 221146

@xuxiapu
Copy link
Contributor

xuxiapu commented Dec 7, 2022

@Juanjo3299 Dear developer,

In this problem, “CountDownLatch” in the thread cannot be used as the basis for whether the thread is released. When “await” waits for 20 seconds, the thread will end.

The reason why CountDownLatch is always 1 is that when importing the same audio for many times, since the audio imported for the first time is still generating the waveform, then the same audio is imported and the waveform is generated. Then, the later imported CountDownLatch will be discarded. At this time, CountDownLatch is useless, but this does not affect the waveform generation of this audio.

To prevent threads from waiting all the time due to "being discarded", you can consider adding the lock or synchronized keyword to generate waveforms in turn when generating waveforms, or you can set the wait duration through await to prevent threads from waiting all the time.

Another way is to maintain a list. When generating waveforms, first determine whether the current audio is generating waveforms.
The code is as follows:
image

@Juanjo3299
Copy link
Author

@xuxiapu Thanks, could you just provide an example of synchronized please.

@xuxiapu
Copy link
Contributor

xuxiapu commented Dec 8, 2022

@Juanjo3299 You can refer to the following sample code. I hope it will be helpful to you.

    new Thread(() -> {
        synchronized (object) {
            latchCountdown = new CountDownLatch(validPath.size());
            updateAudioCache(validPath, latchCountdown);
            try {
                latchCountdown.await();
                SmartLog.i("AudioBase", "all the audio data load complete");

            } catch (InterruptedException e) {
                SmartLog.e("AudioBase", "got exception " + e.getMessage());
            }
        }
        if (getApplicationContext() == null) {
            return;
        }
        isThumbNailTaskEnd = true;

    }).start();

@Juanjo3299
Copy link
Author

@xuxiapu Hello, for now I will leave it in a certain time limit. But it would be of great help if in the example you could already apply the preventive solution of this problem to be able to apply it correctly and efficiently in my project. Thank you very much.

@xuxiapu
Copy link
Contributor

xuxiapu commented Dec 12, 2022

@Juanjo3299 Dear developer,
We plan to optimize it in the next version. Please pay attention to the update of the official website.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants