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

  • Home
  • SEARCH
  • 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 7097971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T10:59:59+00:00 2026-05-28T10:59:59+00:00

I programmed a sudoku solver in Java for a homework, and I am currently

  • 0

I programmed a sudoku solver in Java for a homework, and I am currently trying to figure out the problematic inputs it can face to make it better. I have generated a few thousand sudoku grids with David Bau’s sudoku generator, and now I am running my program against them.

The problem is that while most of them complete in very reasonable times, some of them prove to be problematic and make my algorithm search like crazy until I run out of heap space. So I thought I should offshore the solving job to a secondary thread and run it with a timeout. Right now, I’m using a thread ‘pool’ of one thread (in the form of an ExecutorService) and I’m submitting Callables to it. I then try to get the value with a timeout:

Callable<Long> solveAndReturnTime = new Callable<Long>() { /* snip */ };
Future<Long> time = executor.submit(solveAndReturnTime);
try
{
    long result = time.get(10, TimeUnit.SECONDS);
    System.out.printf("%d millis\n", result);
}
catch (TimeoutException e)
{
    System.err.println("timed out");
    time.cancel(true);
}

My problem is that apparently, one does not simply cancel a Future in Java. Future<T>.cancel(boolean) apparently doesn’t interrupt the task right away. Because of that, the pool is stuck with carrying an undying task, and the subsequent attempts timeout because they never get a chance to run.

Adding more threads to the pool is not an option because I run on limited cores and if too many tasks obstinately run, the legitimate ones will be unfairly slowed down. I also don’t want the overhead of frequently checking if the task was aborted from my main algorithm.

How can I abruptly, mercilessly and brutally terminate a task? I’m open to anything that will let me recover on the main thread.

EDIT My algorithm is completely sequential, uses no global object, and contains no lock. As far as I can tell, nothing will go wrong if the task is cancelled at a random moment; and even if it does, it’s not production code. I’m ready to walk the dangerous and treacherous walk for this one.

  • 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-05-28T10:59:59+00:00Added an answer on May 28, 2026 at 10:59 am

    I believe my case was ‘special’ enough to use Thread.stop, so here is my solution to the people who believe their case is special enough too. (I would take extreme care using that somewhere it could actually matter, though.)

    As mostly everyone points out, there’s no clean way to stop a task without having that task check if it should stop itself. I created a class that implements Runnable to carry out in such a way that it won’t be dramatic if it’s killed. The result field (milliseconds) is an AtomicLong because writes on regular long variables are not guaranteed to be atomic.

    class SolveTimer implements Runnable
    {
        private String buildData;
        private AtomicLong milliseconds = new AtomicLong(-1);
    
        public SolveTimer(String buildData)
        {
            assert buildData != null;
            this.buildData = buildData;
        }
    
        public void run()
        {
            long time = System.currentTimeMillis();
            // create the grid, solve the grid
            milliseconds.set(System.currentTimeMillis() - time);
        }
    
        public long getDuration() throws ContradictionException
        {
            return milliseconds.get();
        }
    }
    

    My code creates a thread on each iteration and runs a SolveTimer. It then attempts to join within 10 seconds. After join returns, the main thread calls getDuration on the run timer; if it returns -1, then the task is taking too long and the thread is killed.

    SolveTimer timer = new SolveTimer(buildData);
    Thread worker = new Thread(timer);
    worker.start();
    worker.join(10000);
    
    long result = timer.getDuration();
    if (result == -1)
    {
        System.err.println("Unable to solve");
        worker.stop();
    }
    

    It should be noted that this makes worker threads harder to debug: when the thread is suspended by the debugger, it can still be killed by Thread.stop(). On my machine, this writes a short error message about ThreadDeath in the console and crashes the Java process.

    There is a possible race condition where the worker thread completes exactly (or right after) getDuration is called, and because of that result will be -1 even if the task actually succeeded. However, that’s something I can live with: 10 seconds is already far too long, so at that point I don’t really care anymore if it’s nearly good enough.

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

Sidebar

Related Questions

I've mostly programmed in C. If I make a binary tree class in C++,
In an embedded application programmed on C on ARM7 (with portability requirements), currently using
Hay guys I've programmed a very simple range finder. The user can only select
I've always programmed in Java, which is probably why I'm so confused about this:
How can a global object be programmed to be treated as a function and
I programmed something like paint. I have JPanel and I can draw on it.I'm
I have programmed a little application in which you can register and edit your
I have never programmed web sites. I know that I can create web sites
I programmed a Web Application with Java EE. I am using log4j and Tomcat
Having programmed microcontrollers before and being interested in trying my hand at building an

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.