• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

new to threads

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have create a thread pool which creates two threads at a time. My program breakes a arraylist into two sublists and then each thread iterates over one list & adds result to a syncronized set and finally I create another thred to write the contents the sync set to a text file.
The problem is whenver one of the two list iterator thread finishes the write to file thread runs and prints incomplete output to a file.
I would like to know if there is a way where in i could write the result to file when both the threads finished their work?
Also the main method/thread finshes before the other threads are done processing can I make the main thread wait till other threads finish execution?
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch, Rony Dsouza!

I would like to know if there is a way where in i could write the result to file when both the threads finished their work?
Also the main method/thread finshes before the other threads are done processing can I make the main thread wait till other threads finish execution?


You have to make the main thread waiting for those two threads... Then you can continue the main thread. There can be few options on how to implement this. Can you show us how you have implemented it so far?
 
Greenhorn
Posts: 4
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use Join(). This makes main thread to wait till other threads are done.
 
Rony Dsouza
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vijitha Kumara wrote:Welcome to CodeRanch, Rony Dsouza!

I would like to know if there is a way where in i could write the result to file when both the threads finished their work?
Also the main method/thread finshes before the other threads are done processing can I make the main thread wait till other threads finish execution?


You have to make the main thread waiting for those two threads... Then you can continue the main thread. There can be few options on how to implement this. Can you show us how you have implemented it so far?



Thanks Vijitha. Below is what I have done till now. This is a sample code that I am using.
As of now I have created a third thread and making it wait till complete count is 2 before it prints the file. It works most of the times but sometimes it stays in the while loop for a long time. I am new to java so may be I am taking the wrong approach.

 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your last thread is very inefficient ...



Think about what this is doing it attempting to murder the CPU checking why the threads haven't finished (they are being delayed by this thread burning lots of CPU unless you have enough cores), its like a nagging child in a car saying are we there yet i.e. putting a sleep or yield in the loop would help though better solutions exist i.e. really this thread should be waiting for a signal/ joining whatever.

Have a look at Java 7's fork / join for instance ...
 
Saloon Keeper
Posts: 14011
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris Hurst wrote: its like a nagging child in a car saying are we there yet



I love this. Do you mind if I use that analogy from now on?
 
Rony Dsouza
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris Hurst wrote:Your last thread is very inefficient ...



Think about what this is doing it attempting to murder the CPU checking why the threads haven't finished (they are being delayed by this thread burning lots of CPU unless you have enough cores), its like a nagging child in a car saying are we there yet i.e. putting a sleep or yield in the loop would help though better solutions exist i.e. really this thread should be waiting for a signal/ joining whatever.

Have a look at Java 7's fork / join for instance ...




I agree that this is not a right approach that's why I had to post it here to get a proper solution.
Can you change/optimize the code to show what I could do so that I get desired output.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic