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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:14:09+00:00 2026-06-03T03:14:09+00:00

Say I have class A and class B. class A has just a main

  • 0

Say I have class A and class B. class A has just a main method with the following code:

public class A{
    public static void main(String[] args){
        String xput = "";
        ExecutorService pool = Executors.newFixedThreadPool(4);
        for(int i = 1; i < number; i++){
            pool.submit(new B(list.get(i-1)));
            xput = B.returnValue;
            System.out.println(xput);//testing purposes
        }
    }
}

class B extends Thread, and looks like this:

public class B extends Thread{

    static String returnValue = "";        

    public B(String x){
        super(x);
    }

    public void run(){
        double x = 20;
        returnValue += "Grand total: " +
            NumberFormat.getCurrencyInstance().format(x) + "\n";
    }
}

Yet System.out.println(xput) does not print anything except an empty line. Anyone know why? My classes have a lot more code than this obviously, but since I’m not getting any output, I’m starting with a small case.

  • 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-03T03:14:10+00:00Added an answer on June 3, 2026 at 3:14 am

    This code suffers from a number of race conditions since they are all updating the same static String returnValue. Also the threads may actually not have run yet when the System.out.println(xput) is called. You would need to use the future.get() method to wait for each of the threads to finish and you can’t do that in the same loop where you submit them to the thread-pool.

    Since 4 threads will be running at once, and they all are updating the same static field, you need to provide some synchronization around that variable. What I would recommend instead is to use the Future features of the ExecutorService instead of modifying the static field. Something like the following should work:

    List<Future<String>> futures = new ArrayList<Future<String>>();
    for(int i = 1; i < number; i++){
        B b = new B(list.get(i - 1));
        // submit the job b add the resulting Future to the list
        futures.add(pool.submit(b));
    }
    // all of the jobs are submitted now
    StringBuilder sb = new StringBuilder();
    for (Future<String> future : futures) {
       // now join with each of the jobs in turn and get their return value
       sb.append(future.get());
    }
    System.out.println(sb.toString());
    
    // you should implement Callable _not_ extend thread
    public class B implements Callable<String> {
        public String call(){
            ...
            return "some string";
        }
    }
    

    The Future features of the ExecutorService allow you to get results from each of the jobs that were processed by the thread-pool. You can submit() Callable classes which can return a result String (or other object) from the call() method.

    Also, your B should implement Callable not extend Thread. Although it would work, it is only because Thread implements Runnable as well. The thread-pool has its own internal threads and you only submit Runnable or Callable objects to it.

    Lastly, instead of using a for (int i loop when processing lists (or any Java collection), you should get into the habit of using:

     for(String x : list) {
        B b = new B(x);
        ...
    

    If you have to use the for (int i then at least go from 0 to the size() of the list:

     for(int i = 0; i < list.size(); i++) {
    

    That way if you change the size of list you don’t have to remember to change your loop as well.

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

Sidebar

Related Questions

Lets say I have the following DataMapper resources: class Post include DataMapper::Resource has n,
I have the following setup. Class, say, Car that has a CarPart (belongsTo=[car:Car]). When
Let's say i have a class Foo and it has a static member variable
Say I have a class Customer which has a property FirstName . Then I
Let's say we have a class foo which has a private instance variable bar
Let's say I have a Size class which has height and width properties (in
Let's say I have an class called Star which has an attribute color .
To keep things simple, lets say I have a Node class, each node has
Pseudo-situation: have a class (let's say BackgroundMagic ), and it has Start() and Stop()
I have a custom class say 'MyCanvas' derived from wpf Canvas class. MyCanvas has

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.