Use LinkedBlockingQueue
import java.io.DataOutputStream; import java.io.FileOutputStream; import java.util.concurrent.LinkedBlockingQueue; public class Test { public static void main(String[] args) throws Exception { LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>(); FileOutputStream fos = new FileOutputStream("out.log"); DataOutputStream dos = new DataOutputStream(fos); while (!queue.isEmpty()) { dos.writeUTF(queue.take()); } } }