0

I am writing a multithreaded Java application which gets info from a websocket and bufferized it on a LinkedList, and I have another thread which gets this info from this linked list, like a queue, first in first out, and also I have a Thread.sleep(1) to make it wait 1 millisecond before it tries to get the info from the linked list again. Sometime I don't know what is happening but it seems to wait more than 1 millisecond after some hours, like the consumer thread is down or too slow. Is there any better alternative than Thread.sleep? Should I bufferize this info using another data structure? By the way this LinkedList is inside a singleton so I can produce the info in one thread and consume in another.

Thanks

Vivek Pakmode
  • 1,016
  • 2
  • 10
  • 22
  • Hi, welcome to Stack Overflow. [Check out how to ask a good question](https://stackoverflow.com/help/how-to-ask). Have you tried solving the problem and ran into an obstacle? Paste the code you're having problems with to clarify! – Tristo Sep 08 '18 at 10:19
  • Actually I am debbuging it for a week, and I dont knwo if I am using a correct Data Structure to handle buffers. Or Thread.sleep is not reliable. – Allan Romanato Sep 08 '18 at 11:40

1 Answers1

1

Thread.sleep is not accurate especially at 1ms resolution. See How accurate is Thread.sleep? and What exactly is RTSJ, the Real-Time Specification for Java? You will need to look into real-time programming to achieve this.

AlexC
  • 1,395
  • 14
  • 26