简体   繁体   中英

how can i time my 2 threads in java

I have 2 threads, one to take Orders and the other one to distribute the orders.

My first thread must be able to get the orders gradually one by one from a file and display it in a JTextArea-1, while this is happening the other thread should be able to remove one order at a time from JTextArea-1 and paste it to another JTextArea-2.

I have created 2 threads and used MVC pattern (with 2 views and 1 model). The Threads are both displaying the values in the JTEXTAREA's at the same time which is not acceptable. How can i solve this issue?

I should have a minimum 5 seconds delay between the removing from JTextArea-1 and pasting into JTextArea-2. kindly help.

Fundamental lack of comprehension of concurrency alert!

This is not the appropriate way to deal with your problem, which probably doesn't even need threads in the first place.

But if you insist on using threads, what you want is a semaphore to block one thread until a condition for the other thread to act exists. A "FIFO blocking queue" would be appropriate here as well.

Using pauses and timing with concurrency is not effective as it is a non-deterministic system, and only lead to even more subtle failure scenarios.

您可以简单地添加一个记录文本的synchronized调用,以便一次只有 1 个线程可以执行该调用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM