This paper studies the system of communicating threads, which can be modeled as a set of task chains with each thread segment described as a task. Different from conventional approaches that assume the communication only happens at the end of thread executions, we assume a very generic scenario that the communication can happen at anytime during the execution. Under this scenario, the timing analysis and the priority assignment require carefully studying the possible execution dependencies among thread segments. We provide a more accurate timing analysis for the case that threads are communicating with message-based mechanisms. We prove that this analysis and the previously proposed one for synchronous communication are compliant with Audsley's algorithm, which opens the possibility to use it to find an optimal priority assignment. We further propose two priority assignment algorithms that maximize the system's robustness metrics. Finally, we evaluate our contributions in terms of both synthetic benchmarks and an industrial case study.