Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

443
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!