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 8515151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:04:14+00:00 2026-06-11T05:04:14+00:00

So, I see a few questions on stackoverflow asking in one way or another

  • 0

So, I see a few questions on stackoverflow asking in one way or another how to “kill” a future, a la the deprecated Thread.stop(). I see answers explaining why it’s impossible, but not an alternative mechanism to solve similar problems.

For example: Practical use of futures? Ie, how to kill them?

I realize a future can’t be “killed.”

I know how I could do this the Java way: break up the task into smaller sleeps, and have some “volatile boolean isStillRunning” in a thread class which is periodically checked. If I’ve cancelled the thread by updating this value, the thread exits. This involves “shared state” (the isStillRunning var), and if I were to do the same thing in Scala it wouldn’t seem very “functional.”

What’s the correct way to do solve this sort of problem in idiomatic functional scala? Is there a reasonably concise way to do it? Should I revert to “normal” threading and volatile flags? I should use @volatile in the same way as the Java keyword?

  • 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-11T05:04:15+00:00Added an answer on June 11, 2026 at 5:04 am

    I think I’ve found a better solution to my own problem. Instead of using a volatile variable to let an operation know when to die, I can send a higher-priority exit message to the actor. It looks something like this:

    val a = new Actor() { 
      def act():Unit = {
        loop{ react {
          case "Exit" => exit(); return;
          case MyMessage => {
            //this check makes "Exit" a high priority message, if "Exit" 
            //is on the queue, it will be handled before proceeding to 
            //handle the real message.
            receiveWithin(0) {
              case "Exit" => exit(); return
              case TIMEOUT => //don't do anything.
            }
            sender ! "hi!" //reply to sender
          }
        }}
      }
    }
    
    a.start()
    val f = a !! MyMessage
    while( ! f.isSet && a.getState != Actor.State.Terminated ) {
      //will probably return almost immediately unless the Actor was terminated
      //after I checked.
      Futures.awaitAll(100,f)
    }
    if( a.getState != Actor.State.Terminated ) {
      f() // the future should evaluate to "hi!"
    }
    a ! "Exit" //stops the actor from processing anymore messages.
               //regardless of if any are still on the queue.
    a.getState // terminated
    

    There’s probably a cleaner way to write this.. but that’s approximately what I did in my application.

    The reactWithin(0) is an immediate no-op unless there is an “Exit” message on the queue. The queue’d “Exit” message replaces the volatile boolean I would have put in a threaded Java application.

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

Sidebar

Related Questions

I see more than a few questions asking how to read metadata from an
I have a few pages and want that they use one style. See in
I see a few questions about this here, but I can't really understand what
I was just viewing stackoverflow page source and I have a few simple questions;
First of all, I know there are a few other StackOverflow questions about this
When reading questions on Stack Overflow or Stack Programmers, I often see answers which
Firstly, Modifying may be the wrong term, I see a few people have posted
I watched few videos/webcasts about Oslo but I still fail to see how it
I have a few thoughts on this but I can see problems with both.
I have written a few lines of code in Python to see if I

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.