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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:17:36+00:00 2026-05-10T21:17:36+00:00

I’ve written a program that counts lines, words, and characters in a text: it

  • 0

I’ve written a program that counts lines, words, and characters in a text: it does this with threads. It works great sometimes, but not so great other times. What ends up happening is the variables pointing to the number of words and characters counted sometimes come up short and sometimes don’t.

It seems to me that the threads are sometimes ending before they can count all the words or characters that they want to. Is it because these threads go out of scope when the while (true) loop breaks?

I’ve included the code from the thready part of my problem below:

private void countText() {   try {     reader = new BufferedReader(new FileReader('this.txt'));     while (true) {       final String line = reader.readLine();       if(line == null) {break;}       lines++;       new Thread(new Runnable() {public void run() {chars += characterCounter(line);}}).start();       new Thread(new Runnable() {public void run() {words += wordCounter(line);}}).start();       println(line);     }    } catch(IOException ex) {return;}  } 

(Sub Question: This is the first time I’ve asked about something and posted code. I don’t want to use StackOverflow in place of google and wikipedia and am worried that this isn’t an appropriate question? I tried to make the question more general so that I’m not just asking for help with my code… but, is there another website where this kind of question might be more appropriate?)

  • 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. 2026-05-10T21:17:37+00:00Added an answer on May 10, 2026 at 9:17 pm

    A different threaded design would make it easier to find and fix this kind of problem, and be more efficient into the bargain. This is a longish response, but the summary is ‘if you’re doing threads in Java, check out java.util.concurrent as soon as humanly possible)’.

    I guess you’re multithreading this code to learn threads rather than to speed up counting words, but that’s a very inefficient way to use threads. You’re creating two threads per line – two thousand threads for a thousand line file. Creating a thread (in modern JVMs) uses operating system resources and is generally fairly expensive. When two – let alone two thousand – threads have to access a shared resource (such as your chars and words counters), the resulting memory contention also hurts performance.

    Making the counter variables synchronized as Chris Kimpton suggests or Atomic as WMR suggests will probably fix the code, but it will also make the effect of contention much worse. I’m pretty sure it will go slower than a single-threaded algorithm.

    I suggest having just one long-lived thread which looks after chars, and one for words, each with a work queue to which you submit jobs each time you want to add a new number. This way only one thread is writing to each variable, and if you make changes to the design it’ll be more obvious who’s responsible for what. It’ll also be faster because there’s no memory contention and you’re not creating hundreds of threads in a tight loop.

    It’s also important, once you’ve read all the lines in the file, to wait for all the threads to finish before you actually print out the values of the counters, otherwise you lose the updates from threads that haven’t finished yet. With your current design you’d have to build up a big list of threads you created, and run through it at the end checking that they’re all dead. With a queue-and-worker-thread design you can just tell each thread to drain its queue and then wait until it’s done.

    Java (from 1.5 and up) makes this kind of design very easy to implement: check out java.util.concurrent.Executors.newSingleThreadExecutor. It also makes it easy to add more concurrency later on (assuming proper locking etc), as you can just switch to a thread pool rather than a single thread.

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

Sidebar

Ask A Question

Stats

  • Questions 87k
  • Answers 87k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer MySQL stores your query history in the file ~/.mysql_history. Certain… May 11, 2026 at 5:32 pm
  • Editorial Team
    Editorial Team added an answer The quickest way is to use DROP TABLE to drop… May 11, 2026 at 5:32 pm
  • Editorial Team
    Editorial Team added an answer Above this line: return false; Add this: $('#credits').load($(this).attr('alt')); The whole… May 11, 2026 at 5:32 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.