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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:31:32+00:00 2026-05-26T06:31:32+00:00

I have this code : public void GenerateWtW() { ExecutorService exec = Executors.newFixedThreadPool(30); ConcurrentHashMap<String,

  • 0

I have this code :

public void GenerateWtW() {
        ExecutorService exec = Executors.newFixedThreadPool(30);

        ConcurrentHashMap<String, Double> tf_idfCache = new ConcurrentHashMap<String, Double>();
        ArrayList<String> allwords = getAllWords();
        int no_docs = getNumberOfDocs();

        int cnt = 0;
        for (int i = 0; i < allwords.size(); i++) {
            String word1 = allwords.get(i);
            if (i < allwords.size() - 1) {
                for (int j = i + 1; j < allwords.size(); j++) {
                    String word2 = allwords.get(j);
                    cnt++;
                    if (word1.equals(word2)) {
                        continue;
                    }

                    //System.out.println("[" + cnt + "] WtW Started: " + word1 + "," + word2 + " No of Docs: " + no_docs + " Total No of words: " + allwords.size());
                    WTWThread t = new WTWThread(tf_idfCache, word1, word2, this, no_docs, db);
                    exec.execute(t);

                }
            }
        }
        exec.shutdown();
    }

and here is the code for the thread:

private static class WTWThread implements Runnable {

        private ConcurrentHashMap<String, Double> cacheRef;
        private String word1, word2;
        private WordRank workRankInstance;
        private int no_docs;
        private Database db;

        public WTWThread(ConcurrentHashMap<String, Double> cacheRef, String word1, String word2, WordRank workRankInstance, int no_docs, Database db) {
            this.cacheRef = cacheRef;
            this.word1 = word1;
            this.word2 = word2;
            this.workRankInstance = workRankInstance;
            this.no_docs = no_docs;
            this.db = db;
        }

        @Override
        public void run() {
            double sum = 0;

            for (int i = 1; i <= 10; i++) {
                Double tf_idf1 = cacheRef.get(word1 + i);
                if (tf_idf1 == null) {
                    tf_idf1 = workRankInstance.getTF_IDF(word1, i);
                    cacheRef.put(word1 + i, tf_idf1);
                }
                Double tf_idf2 = cacheRef.get(word2 + i);
                if (tf_idf2 == null) {
                    tf_idf2 = workRankInstance.getTF_IDF(word2, i);
                    cacheRef.put(word2 + i, tf_idf2);
                }
                sum = sum + (tf_idf1 * tf_idf2);
            }
            double wtw = sum / no_docs;
            String query = "INSERT INTO wtw(word1,word2,wtw) VALUES(?,?,?);";
            try {
                PreparedStatement ps = db.getConnection().prepareStatement(query);
                ps.setString(1, word1);
                ps.setString(2, word2);
                ps.setDouble(3, wtw);
                ps.executeUpdate();
                ps.close();
            } catch (SQLException ex) {
                Logger.getLogger(WordRank.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

everything to me looks fine but here is what happens, when I run the program it processes the first few hundreds and then suddenly stops ! I checked in the System Monitor, the java process starts growing in memory usage and it goes up to something about 1Gb and then nothing happens. I thought maybe this is happening because I’m having too many threads, I tried with 4 threads but same thing happens. Then I thought maybe I should use sleep() before creating the threads and that did solve the problem, it worked like a charm, but even sleep(1) makes the program very slow ! and I checked every possible thing that I could think of ! Is there anything I’m missing here ?

  • 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-26T06:31:33+00:00Added an answer on May 26, 2026 at 6:31 am

    How many words do you have, how much RAM do you have and what is this program doing?

    Your tf_idfCache will get very large growing at least quadratically with number of words, with quite of constant factor (you are putting 10 things to cache for every word?), and it might cause performance problems.

    Finally you do have a concurrency issue, but I don’t think it is causing a lock. In code

    Double tf_idf1 = cacheRef.get(word1 + i);
    if (tf_idf1 == null) {
        tf_idf1 = workRankInstance.getTF_IDF(word1, i);
        cacheRef.put(word1 + i, tf_idf1);
    }
    

    You have no guarantee that you won’t calculate rank twice.

    I don’t think that number of threads is causing any problem, but you might have some other concurrency issue that is causing a lock (if locking, and not memory overhead is a problem at all).

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

Sidebar

Related Questions

I have this code: hubSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int
Say, I have a code snippet like this: public static void main(String[] args) {
I have this code: public void Contacts(string domainToBeTested, string[] browserList, string timeOut, int numberOfBrowsers)
I have this code: public void FileCleanup(List<string>paths) { string regPattern = (@[~#&!%+{}]+); string replacement
I have written this piece of code public class Test{ public static void main(String[]
I have this code in c# public void startRecognition(string pName) { presentationName = pName;
I have this code in my C# project: public void startRecognition(string pName) { presentationName
I have this code: public void AddNode(string Node) { try { treeView.Nodes.Add(Node); treeView.Refresh(); }
I have this simple code: public void Run() { var invokerThread = new Thread(new
i have this code: public class Test{ arrayList<String> list = new ArrayList<String>(); String[][] temp_list;

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.