using a lock on the stream shared by both threads to prevent possible problems