my dog learned polymorphism*
The moose likes Threads and Synchronization and the fly likes new to threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "new to threads" Watch "new to threads" New topic
Author

new to threads

Rony Dsouza
Greenhorn

Joined: Aug 15, 2011
Posts: 3
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?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

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?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
srikanth kumar aruru
Greenhorn

Joined: Jul 21, 2011
Posts: 4

use Join(). This makes main thread to wait till other threads are done.


André Bensoussan once explained the difference between a programmer and a designer:

"If you make a general statement, a programmer says, 'Yes, but...'
while a designer says, 'Yes, and...'"
Rony Dsouza
Greenhorn

Joined: Aug 15, 2011
Posts: 3
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.

Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 417
    
    2

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 ...


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

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

Joined: Aug 15, 2011
Posts: 3
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: new to threads