dear all i am quite new to java multithreading and i am trying to create a thread that read a byte array from a streaming thread that streams in an array of bytes withen 10 seconds my new thread should be able to read a data each secound and sleep for one secound and then read another deta and compare the them at the end
Think of a cookie jar as a shared resource. You and I both agree that when the lid
is not on the jar we simply wait until someone else gives us a push. You are a
cookie supplier and I am a cookie monster (I eat those cookies!). Only one of
us can grab the lid from that jar (we don't fight for it and we play it fair).
When I can grab the lid from that jar and I find a cookie, I'll eat it and put that
lid back on. If I don't find a cookie I simply put that lid back on and wait.
When you (the cookie producer) can grib the lid from that jar and you don't find
a cookie in there you put a cookie in the jar and put that lid back on. If you find
a cookie in the jar you just put that lid on the jar again.
Putting the lid back on that jar makes a lot of noise and activates anyone who
was waiting for that event.
Suppose we start with an empty jar and I was first. I open the jar, see nothing
and put the lid back on and wait. The you come: you find the closed empty jar
put a cookie in and close that jar again. I sort of wake up, find the lid on the
jar, open it and eat the cookie.
That lid is the mutex, which you own or not. The 'synchronized' keyword in java
is the analogy of that lid. If I don't find a cookie I wait. The Object's wait() method
is Java's equivalence of that. Putting that lid on that jar again (with a lot of noise)
is represented in Java by the Object's notify(All)() method.
That cookie represents the bytes written by the producer and consumed by me
the cookie monster: no cookie means no bytes written. A cookie in the jar is
equivalent to written an not yet consumed bytes. You and I are the two different threads.
Does that clarify things a bit?
kind regards,
Jos