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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:55:59+00:00 2026-05-25T16:55:59+00:00

Code that illustrates my problem: public class Linkedlisttest { public static void main(String[] args)

  • 0

Code that illustrates my problem:

public class Linkedlisttest {
    public static void main(String[] args) {
        Linkedlisttest test = new Linkedlisttest();
        test.go(args);
    }
    public void go(String[] args) {
        int cpus = Runtime.getRuntime().availableProcessors();
        ThreadPoolExecutor tpe = new ThreadPoolExecutor(cpus, cpus * 2, 1L,
                TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
        String numbersarray[] = {"one", "two", "three", "four", "five"};
        LinkedList<String> numbers = new LinkedList(Arrays.asList(numbersarray));
        for (int index = 0; index < 2; index++) {
            tpe.execute(new removeNumbers(numbers, index));
        }
    }
    class removeNumbers implements Runnable {
        LinkedList<String> localnumbers;
        int index;
        public removeNumbers(LinkedList<String> localnumbers, int index) {
            this.localnumbers = localnumbers;
            this.index = index;
        }
        @Override
        public void run() {
            System.out.println(localnumbers.size() + " Thread#: " + index);
            while (localnumbers.size() > 0) {
                System.out.println(localnumbers.removeFirst() + " Thread#: " + index);
            }
        }
    }
}

and the output (which varies somewhat in which thread removes what element):

5 Thread#: 0
5 Thread#: 1
one Thread#: 0
three Thread#: 0
four Thread#: 0
five Thread#: 0
two Thread#: 1

I’m expecting {"one", "two", "three", "four", "five"} to get removed twice, once by each thread. However, it seems like the removeNumbers Runnables are sharing the same localnumbers LinkedList. Why does this happen? My understanding is that I’ve created two separate instances of localnumbers, one in each removeNumbers Runnable.

  • 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-25T16:56:00+00:00Added an answer on May 25, 2026 at 4:56 pm

    You’re passing a reference to the same LinkedList<String> into both constructor calls, so each localnumbers is pointing to the same List. That’s why both are removing from the same List, because you haven’t actually copied it.

    You will want to do something along the lines of:

    LinkedList<String> numbers = new LinkedList(Arrays.asList(numbersarray));
    for (int index = 0; index < 2; index++) {
        LinkedList<String> numbersCopy = new LinkedList<String>(numbers);
        tpe.execute(new removeNumbers(numbersCopy, index));
    }
    

    There are more efficient ways to make these copies. Note that even two calls to Arrays.asList() using the same array will not be enough to truly create copies, since that method returns a List backed by the array. You will need to create copies of the List as in the loop above, or else use System.arraycopy() to copy the array at the beginning:

    String[] numbersarray = {"one", "two", "three", "four", "five"};
    String[] numbersarray2 = new String[numbersarray.length];
    System.arraycopy(numbersarray, 0, numbersarray2, 0, numbersarray.length);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a minimum code example that illustrates the problem: #include <iostream> class Thing {
I've got a problem with inheritance and generics. This is the code that illustrates
The code that I want to write is like this: void MethodOnThreadA() { for
Here is the basic gist of my problem: My main Window class instantiates Class
I have a class that creates a List<Action<int>> and holds on to them until
Code that is untestable really annoys me. The following things make oo-code untestable: global
Sample code that shows how to create threads using MFC declares the thread function
I'm maintaining some code that uses a *= operator in a query to a
I'm writing C# code that uses the windows IP Helper API. One of the
I've got C# code that accesses MySQL through ODBC. It creates a transaction, does

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.