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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:25:13+00:00 2026-05-30T10:25:13+00:00

Basically I’ve got a little threading class used by ExecutorService and a fixed thread

  • 0

Basically I’ve got a little threading class used by ExecutorService and a fixed thread pool. Each thread instantiates my threading class and the call method is fired, works great!

However I really need to call another class (via instantiation or static means) to process & return some data within the call method, however when trying this I understandably get concurrent.ExecutionException, along with related methods.

I think it will be easier to paste all my code here, note its very rough

MainController

package com.multithreading.excutorservice;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public class MainController {


    private static List<String> urls;

    public static void main(String[] args) {

        populateList();
        // futures to retrieve task results
        List<Future<ArrayList>> futures = new ArrayList<Future<ArrayList>>();
        // results
        List<ArrayList> results = new ArrayList<ArrayList>();
        // pool with 5 threads
        ExecutorService exec = Executors.newFixedThreadPool(5); 

        // enqueue tasks
        for(String url: urls) {
            futures.add(exec.submit(new ThreadTask(url)));
        }

        // attempt to move ArrayLists within Future<ArrayList> into a normal ArrayList
        for(Future<ArrayList> future: futures) {
            try {
                results.add(future.get());
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


    //  for(ArrayList<String> s: results) {
    //      System.out.println(s);
    //  }
    }

    private static void populateList() {
        urls = new ArrayList<String>();

        urls.add("http://www.google.com");
        urls.add("http://www.msn.co.uk");
        urls.add("http://www.yahoo.co.uk");
        urls.add("http://www.google.com");
        urls.add("http://www.msn.co.uk");
        urls.add("http://www.yahoo.co.uk");
    }

}

ThreadTask

package com.multithreading.excutorservice;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;



public class ThreadTask implements Callable<ArrayList> {
        private String url;
        HtmlParser parseHtml;

        public ThreadTask(String url) {
            this.url = url;
        }

        public ArrayList call() {

            int counter = 0;
            String html = null;

            try {
                URL myUrl = new URL(url);
                BufferedReader reader = new BufferedReader(new InputStreamReader(myUrl.openStream()));

                while ((html = reader.readLine()) != null) {
                    //counter += inputLine.length();
                    html += html;
                    }
                }
                catch (Exception ex) {
                    System.out.println(ex.toString());
                }

                ArrayList<String> storeLinks = new ArrayList<String>();
                HtmlParser par = new HtmlParser();
                storeLinks = par.returnNewUrls(html);

              //  for(String s: parseHtml) {
              //    System.out.println(s);
              //  }

                //returns an ArrayList of URLS which is stored in a List<Future<ArrayList>> temporarily
              return storeLinks;

        }
   }

HtmlParser

package com.multithreading.excutorservice;

import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HtmlParser {

    private final String regex_links = "\\s*(?i)href\\s*=\\s*(\"([^\"]*\")|'[^']*'|([^'\">\\s]+))"; 
    private ArrayList<String> extractedUrls;

    public ArrayList<String> returnNewUrls (String data) {

        extractedUrls = new ArrayList<String>();

        Pattern p = Pattern.compile(regex_links);
        Matcher m = p.matcher(data);
        System.out.println("Test");

        while (m.find()) {
            System.out.println("Test");
            extractedUrls.add(m.group(1));

        }

        return getLinks();
    }

   //returns the links
    public ArrayList getLinks() {
        return extractedUrls;
    }
}
  • 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-30T10:25:15+00:00Added an answer on May 30, 2026 at 10:25 am

    You’re doing some pretty weird stuff here. Multiple threads are accessing the same static extractedUrls field, and each call to returnNewUrls creates a new field. In your returnNewUrls method, create a new ArrayList which is local to the method scope. Something along the lines of:

    public static ArrayList<String> returnNewUrls(String data) {
      ArrayList<String> urls = new ArrayList<String>();
      addStuffToUrlsList();
      return urls;
    }
    

    Another thing – not a bug, but you’re doing unnecessary stuff – in the call method you don’t need to create a new list if you’re just assigning to a variable:

    ArrayList<String> parseHtml = new ArrayList<String>();
    parseHtml = HtmlParser.returnNewUrls(html);
    

    This is better:

    ArrayList<String> parseHtml = HtmlParser.returnNewUrls(html);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
Basically I have a fixed size IFRAME with overflow: auto . The IFRAME displays
Basically, I've seen this used all to often: public event MyEventHandler MyEvent; private void
Basically, I have the following code: public class MyDictionary<TKey, TValue> : IDictionary<TKey, TValue> {
Basically I have a series of projects displayed on the page. Each project consists
Basically what I want to do it this: a pdb file contains a location
Basically, something better than this: <input type=file name=myfile size=50> First of all, the browse
Basically I have some code to check a specific directory to see if an
Basically I'm going to go a bit broad here and ask a few questions
Basically, I would like a brief explanation of how I can access a SQL

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.