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

The Archive Base Latest Questions

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

Here is a simplified version of my application showing what I’m doing. /* in

  • 0

Here is a simplified version of my application showing what I’m doing.

/* in my app's main():      Runner run = new Runner();      run.dowork();  */  class Runner {     private int totalWorkers = 2;     private int workersDone = 0;      public synchronized void workerDone()     {         workersDone++;         notifyAll();     }      public synchronized void dowork()     {         workersDone = 0;          //<code for opening a file here, other setup here, etc>          Worker a = new Worker(this);         Worker b = new Worker(this);          while ((line = reader.readLine()) != null)         {             //<a large amount of processing on 'line'>              a.setData(line);             b.setData(line);              while (workersDone < totalWorkers)             {                 wait();             }                        }     } }  class Worker implements Runnable {     private Runner runner;     private String data;      public Worker(Runner r)     {         this.runner = r;         Thread t = new Thread(this);         t.start();     }      public synchronized void setData(String s)     {         this.data = s;         notifyAll();     }      public void run     {         while (true)         {             synchronized(this)             {                 wait();                  //<do work with this.data here>                  this.runner.workerDone();             }         }     } } 

The basic concept here is that I have a bunch of workers which all do some processing on an incoming line of data, all independently, and write out the data wherever they like – they do not need to report any data back to the main thread or share data with each other.

The problem that I’m having is that this code deadlocks. I’m reading a file of over 1 million lines and I’m lucky to get 100 lines into it before my app stops responding.

The workers, in reality, all do differing amounts of work so I want to wait until they all complete before moving to the next line.

I cannot let the workers process at different speeds and queue the data internally because the files I am processing are too large for this and won’t fit in memory.

I cannot give each worker its own FileReader to independently get ‘line’, because I do a ton of processing on the line before the workers see it, and do not want to have to re-do the processing in each worker.

I know I’m missing some fairly simple aspect of synchronization in Java but I’m stuck at this point. If someone could explain what I’m doing wrong here I would appreciate it. I believe I’m misunderstanding some aspect of the synchronization but I’m out of ideas for attempting to fix it.

  • 1 1 Answer
  • 2 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:57:40+00:00Added an answer on May 10, 2026 at 9:57 pm

    Working directly with synchronized, wait(), and notify() is definitely tricky.

    Fortunately the Java Concurrency API provides some excellent control objects for this sort of thing that are much more intuitive. In particular, look at CyclicBarrier and CountDownLatch; one of them almost certainly will be what you’re looking for.

    You may also find a ThreadPoolExecutor to be handy for this situation.

    Here’s a simple example / conversion of your snippet that produces the following output (without deadlock, of course):

    Read line: Line 1
    Waiting for work to be complete on line: Line 1
    Working on line: Line 1
    Working on line: Line 1
    Read line: Line 2
    Waiting for work to be complete on line: Line 2
    Working on line: Line 2
    Working on line: Line 2
    Read line: Line 3
    Waiting for work to be complete on line: Line 3
    Working on line: Line 3
    Working on line: Line 3
    All work complete!

    public class Runner {      public static void main(String args[]) {         Runner r = new Runner();         try {             r.dowork();         } catch (IOException e) {             // handle             e.printStackTrace();         }     }      CyclicBarrier barrier;     ExecutorService executor;     private int totalWorkers = 2;      public Runner() {         this.barrier = new CyclicBarrier(this.totalWorkers + 1);         this.executor = Executors.newFixedThreadPool(this.totalWorkers);     }      public synchronized void dowork() throws IOException     {         //<code for opening a file here, other setup here, etc>         //BufferedReader reader = null;         //String line;          final Worker worker = new Worker();          for(String line : new String[]{'Line 1', 'Line 2', 'Line 3'})         //while ((line = reader.readLine()) != null)         {             System.out.println('Read line: ' + line);             //<a large amount of processing on 'line'>              for(int c = 0; c < this.totalWorkers; c++) {                 final String curLine = line;                 this.executor.submit(new Runnable() {                     public void run() {                         worker.doWork(curLine);                     }                 });             }              try {                 System.out.println('Waiting for work to be complete on line: ' + line);                 this.barrier.await();             } catch (InterruptedException e) {                 // handle                 e.printStackTrace();             } catch (BrokenBarrierException e) {                 // handle                 e.printStackTrace();             }         }          System.out.println('All work complete!');     }      class Worker     {         public void doWork(String line)         {             //<do work with this.data here>             System.out.println('Working on line: ' + line);              try {                 Runner.this.barrier.await();             } catch (InterruptedException e) {                 // handle                 e.printStackTrace();             } catch (BrokenBarrierException e) {                 // handle                 e.printStackTrace();             }         }     }     } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a simplified version of my class: public abstract class Task { private static
Here is simplified version of my requirement I have a java class say Processor
Here is a simplified version of one of my models: class ImportRule(models.Model): feed =
Here is a simplified version of something I'm trying to run: for (var i
I'm currently doing a firewall management application for Django, here's the (simplified) model :
Here's a simplified version of what I'm trying to do : Before any other
Here is a simplified version of my database model. I have two tables: Image,
Here is a simplified version of what I have (not working): prog.h: ... const
Here is a simplified version of the code I use. I need to rewrite
Here is a simplified version of what I'm trying to do: Without having multiple

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.