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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:13:17+00:00 2026-06-11T00:13:17+00:00

I’m just learning a bit about threads in Java and I was wondering if

  • 0

I’m just learning a bit about threads in Java and I was wondering if some one could help me out.

I have created a list of 10 integers. What I want to do is have multiple threads go in, grab the integer at index 0, print it and remove it. I want this to happen until there are no more numbers in the list. This is my code so far.

public class SlothTest implements Runnable{

static ArrayList<Object> test = new ArrayList<>();
static int listSize;

public static void main(String[] args) {

    for (int i = 0; i < 10; i++){
        test.add(i);
    }

    SlothTest runner = new SlothTest();
    Thread alpha = new Thread(runner);
    Thread beta = new Thread(runner);
    alpha.setName("Alpha thread");
    beta.setName("Beta thread");
    alpha.start();
    beta.start();
}

@Override
public void run() {
    listSize = test.size();
    while (listSize > 0){
        getLink();
    }
}

private synchronized void getLink(){
    String threadName = Thread.currentThread().getName();
    System.out.println(threadName + " printed " + test.indexOf(listSize - 1));
    test.remove(0);
    listSize = test.size();
}

}

Can someone help point out everything I’m doing wrong, it’s probably a lot.

  • 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-11T00:13:19+00:00Added an answer on June 11, 2026 at 12:13 am

    The test of listSize > 0 is not properly synchronized. The list could be non-empty when the check is performed but then actually be empty by the time the thread calls getLink(). It would probably be easier to have getLink() return boolean instead of void where true would indicate that the list is empty and false would indicate that it is not then each thread could just keep calling the method until it returns false. You would also have to check that the list is not empty at the top of of getLink() before you try to remove an item.

    @Override
    public void run() {
        while (getLink());
    }
    
    private synchronized boolean getLink(){
        String threadName = Thread.currentThread().getName();
        System.out.println(threadName + " printed " + test.indexOf(test.size() - 1));
        if(!test.isEmpty()){
            test.remove(0);
        }
        return !test.isEmpty()
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
This could be a duplicate question, but I have no idea what search terms
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't

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.