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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:53:01+00:00 2026-06-03T04:53:01+00:00

Exception in thread Thread-8 java.lang.StackOverflowError at sun.misc.Unsafe.compareAndSwapLong(Native Method) at java.util.concurrent.atomic.AtomicLong.compareAndSet(Unknown Source) at java.util.Random.next(Unknown Source)

  • 0
Exception in thread "Thread-8" java.lang.StackOverflowError
    at sun.misc.Unsafe.compareAndSwapLong(Native Method)
    at java.util.concurrent.atomic.AtomicLong.compareAndSet(Unknown Source)
    at java.util.Random.next(Unknown Source)
    at java.util.Random.nextInt(Unknown Source)
        at sim.ant.colony.ants.Forager.moveTo(Forager.java:108)

I’m working with a multi threaded application(Ant Simulation Colony) where I created every ant as a thread and when tens of threads (ants) are running, above exception occurs after every few turns and thread is killed. code where I’m using Random() is;

Random rand = new Random();
return adjacents.elementAt(rand.nextInt(8));

this code is written in a thread’s local member function. So assuming every thread creates random numbers hundred of times and there are more than ten threads are working.

Can somebody help me with this exception?

  • 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-03T04:53:03+00:00Added an answer on June 3, 2026 at 4:53 am

    Not really an answer to your problem but more an example of the problem you have, how you can track it and how you can fix it.

    Take the code below:

    import java.util.HashMap;
    import java.util.Map;
    
    public class StackOverflowExample {
    
        public static void a() {
            Map<String, String> map = new HashMap<String, String>();
            map.putAll(b());
    
        }
    
        private static Map<String, String> b() {
            Map<String, String> map = new HashMap<String, String>();
            map.put("a", "a");
            a();
            return map;
        }
    
        public static void main(String[] args) {
            a();
        }
    }
    

    It will produce a StackOverFlowError instantly:

    Exception in thread "main" java.lang.StackOverflowError
        at java.util.HashMap$Entry.<init>(Unknown Source)
        at java.util.HashMap.addEntry(Unknown Source)
        at java.util.HashMap.put(Unknown Source)
        at StackOverflowExample.b(StackOverflowExample.java:14)
    

    The problem is not in HashMap, nor HashMap$Entry. The problem is that a() and b() call each other recursively without a proper stop-condition, meaning infinitely. If you actually look below in the stack, you immediately discover that pattern:

    Exception in thread "main" java.lang.StackOverflowError
        at java.util.HashMap$Entry.<init>(Unknown Source)
        at java.util.HashMap.addEntry(Unknown Source)
        at java.util.HashMap.put(Unknown Source)
        at StackOverflowExample.b(StackOverflowExample.java:14)
        at StackOverflowExample.a(StackOverflowExample.java:8)
        at StackOverflowExample.b(StackOverflowExample.java:15)
        at StackOverflowExample.a(StackOverflowExample.java:8)
        at StackOverflowExample.b(StackOverflowExample.java:15)
        at StackOverflowExample.a(StackOverflowExample.java:8)
        at StackOverflowExample.b(StackOverflowExample.java:15)
        at StackOverflowExample.a(StackOverflowExample.java:8)
        at StackOverflowExample.b(StackOverflowExample.java:15)
        at StackOverflowExample.a(StackOverflowExample.java:8)
        at StackOverflowExample.b(StackOverflowExample.java:15)
            ...
    

    You need to find the recursion that goes too deep in your code. Either fix it or change it to a non-recursive method.

    Using a debugger can be of great help for that as you can step into each method call and put breakpoints (amongst plenty of other good stuffs).

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

Sidebar

Related Questions

Exception in thread Main java.lang.UnsatisfiedLinkError: com.sun.midp.main.Configuration.getProperty0 (Ljava / lang / String;) Ljava / lang
Exception in thread Thread-0 java.lang.NullPointerException at org.apache.http.impl.nio.reactor.AbstractIOReactor.closeActiveChannels(AbstractIOReactor.java:532) at org.apache.http.impl.nio.reactor.AbstractIOReactor.hardShutdown(AbstractIOReactor.java:564) at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.doShutdown(AbstractMultiworkerIOReactor.java:411) at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.execute(AbstractMultiworkerIOReactor.java:340) at
Exception in thread Thread-2 java.lang.NumberFormatException: For input string: 3 int test = Integer.parseInt(result[0]); This
Exception in thread main java.lang.ClassCastException: org.apache.derby.client.am.Statement cannot be cast to java.beans.Statement at CollegeLibrary.NotRegistered.DBStatement(NotRegistered.java:114) at
I am getting something like the following: Exception in thread main java.lang.UnsatisfiedLinkError: no viewerNativeDLL
I am getting the following error Exception in thread main java.lang.ClassCastException: org.hibernate.impl.SessionFactoryImpl cannot be
I get the following error in my java code: Exception in thread main java.lang.StringIndexOutOfBoundsException:
This is the error I keep getting. Exception in thread Thread-3 java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:782)
I have the following Exception: Exception in thread main java.lang.SecurityException: no manifiest section for
Here is My Exception Exception in thread main java.lang.IllegalArgumentException: Property 'sessionFactory' is required at

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.