3 ms·
Inter-thread communication through volatile fields is error-prone. For most multithreaded applications, a better way is to use Queue objects to communicate bet
by csense 14y ago
Inter-thread communication through volatile fields is error-prone. For most multithreaded applications, a better way is to use Queue objects to communicate between threads. The typical pattern for multithreaded programming is: A worker thread receives work in an "inbox" queue, processes it, and puts results in an "outbox" queue. When the main program wants to shut down the thread, it sends a special object to the "inbox" queue.
Common variations on the theme include pipelined processing (where one worker thread's outbox is another worker thread's inbox) or result gathering (every worker thread writes to the same outbox queue, and another thread reads the outbox queue and writes the results sequentially to a file, database, or socket).