passing data between threads

Hello,
I would like to know what is the safest way of communicating between threads in java.
I have four threads that collect udp packets on different ports. I would like the data from all threads written to file through a single thread.
I used a simple communication mechanism using shared object and booleans to wait and exit the synchronized method but this does not seem to work all the time.

I would like to know if there is a safe way of passing arrays between threads and communicating.

Define 'doesn't work all the time'...you should synchronize on the object/methods accessed between the threads (no example code, no way to tell what exactly is going on). If you wish to write out to a file as you describe, create a thread responsible for this task which contains a queue of the data to write - other threads add to the queue (in a synchronized manner). This writer thread can then wait (if queue is empty) and get notified when it has something to write, then proceed through each object in the queue, writing the contents to the file.

"Does not work all the time" ...i am trying to write same data in all the files but when i try to read out the data from the file through separate program some of the data that is read out is not the same in all the files even though most of the times it is the same.

I would really appreciate if you can help me out .
I dont know about blocking queue...if you can suggest any tutorial i would read it.