https://kotlinlang.org logo
#codereview
Title
# codereview
d

Dalinar

12/26/2018, 9:54 AM
could someone proof-read this? it seems to work, I just want to make sure that I understand
CompletableFuture
correctly if I hammer (just from a single thread)
asyncDumpReport()
then am I correct to assume that there is no chance of
dumpReport()
firing until the previous call to
dumpReport()
has completed?
Copy code
`
    /**
     * calls [dumpReport] asynchronously, _if there is not currently such a call in progress_
     */
    private fun asyncDumpReport() {
        val p = promise
        if (p == null || p.getNow(false))
            promise = CompletableFuture.supplyAsync {
                try {
                    dumpReport()
                } finally {}
                true
            }
    }
    private var promise: CompletableFuture<Boolean>? = null
`
a

Andreas Sinz

12/26/2018, 10:24 AM
Yes, if you call
asyncDumpReport
while another dump is still running, it will just skip the dumping-part
d

Dalinar

12/26/2018, 10:25 AM
great, thanks
t

Timmy

12/27/2018, 11:19 AM
The thread safety seems dubious to me me, what if two threads call at the same time and both check the if statement before any of them have set the promise? Then you have two dump reports running at the same time. Or if promise is assigned to while another thread is between
val p =...
and the if? Since you not using the result of CompletableFuture you're could instead of use the thread safe functions from AtomicBoolean for the example
6 Views