Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9247929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:51:35+00:00 2026-06-18T09:51:35+00:00

I want a thread-safe (immutable) Scala class for a long working task. For some

  • 0

I want a thread-safe (immutable) Scala class for a long working task. For some corner cases the tasks takes very long so I want to implement a time-out. What’s the best way to implement this in an immutable class?

My first attempt was to use an implicit parameter like this:

class Worker(val input: String) {

  def start: String = {
    implicit val startTimeStamp = new TimeStamp
    doSomething1 + doSomething2
  }

  def doSomething1()(implicit startTimeStamp: TimeStamp): String = { ... }

  def doSomething2()(implicit startTimeStamp: TimeStamp): String = {
    ... 
    checkTimeout 
    ...
   }
}

class TimeStamp { val start = System.currentTimeMillis }

This should work, but there is still a lot of boiler-plate code with the implicit parameters. (In the real code I have hundreds deeply nested doSomething-functions in the worker class.) Is there a more beautiful way to do this in Scala?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:51:36+00:00Added an answer on June 18, 2026 at 9:51 am

    Sounds like you are looking for futures. In scala 2.9.x I would suggest you to use the akka library for that, from 2.10.0 on there is the scala.concurrent.Future trait.

    example for 2.10:

    import concurrent._
    import concurrent.duration._
    import ExecutionContext.Implicits.global
    
    val f = future {
      blocking {
        // long running task
      }
    }
    
    try {
      val res = Await.result(f, 100 millis)
    } catch {
      case e: java.util.concurrent.TimeoutException =>
        // handle timeout
    }
    

    edit: added blocking call as suggested by Viktor Klang.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make my class thread-safe without large overhead. The instances will be
I am writing some code to solve thread-safe issues in a system am working
I am writing an Objective-C class that I want to be thread safe. To
I want to know if the following class is thread safe and works perfectly
Let's say I want to create a collection class that is thread-safe by default.
Consider following code, I want to make it a thread safe class, so that
I want to create a Thread safe JSP page. It is possible in Servlet
I want to know that is boost::asio::deadline_timer thread safe? Can somebody answer me?
I understand that for a CPU-bound task that you typically want 1 thread per
I know the API state A thread safe client for working with your SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.