Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

446
Visualizações
Is it good idea to use atomic boolean in kotlin?

Hey I am learning in atomic in kotlin. I am wondering is this good idea to use atomic boolean in my scenario? Can someone suggest, how to do in atomic way.

Scenario 1 Not for first time call

var isFirstTime = true

fun notForFirstTime(){
   if(!isFirstTime){
     jobDone()
   }
  isFirstTime = false
}

Scenario 2 only for first time

var isFirstTime = true

fun onlyForFirstTime(){
   if(isFirstTime){
     jobDone()
   }
  isFirstTime = false
}

Can I do this in atomic way? Also is this good idea?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

It depends on what you're trying to achieve.

For scenario 2, most of the time what you want is a value that's computed once and reused later. For this use-case, you can use lazy delegates like this:

class MyClass {
    
    // will be computed the first time it's accessed, and then reused
    val value by lazy { computeExpensiveValue() }
}


fun computeExpensiveValue(): Int {
    // some complex stuff
    return 42
}

I never encountered scenario 1, but in cases where lazy is not sufficient you could indeed use atomic booleans for CAS approaches, or locks. Check for instance kotlinx.atomicfu.

If you're using Kotlin coroutines, you might want to check other synchronization primitives like Mutex and Semaphore. But in general it's preferable to make coroutines communicate over channels rather than sharing mutable state.

over 4 years ago · Santiago Trujillo Relatório

0

It is not very clear what do you ask. But assuming you want to make sure some code is executed (or not executed) only for the first time and the function may be invoked concurrently (because you asked for "atomic") then the answer is: no and no. Both your code samples aren't thread-safe and may result in more than the first call to be handled in a special way.

This is not because boolean is not atomic, but rather because your code is not atomic. Two or more concurrent threads may check for isFirstTime at the same time, before other thread will flip it to false.

You can solve this in multiple ways, for example using mutexes, but probably the easiest and most performant is to use one of compare and swap operations. For example:

val isFirstTime = AtomicBoolean(true)

fun notForFirstTime(){
  if (!isFirstTime.getAndSet(false)) {
    jobDone()
  }
}

fun onlyForFirstTime(){
  if (isFirstTime.getAndSet(false)) {
    jobDone()
  }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda