If you read the online documents, then you must spot
out Java is operated on a multithreading info access
level.
> I have used a synchronized block to read and update
> the integer, and
> the result is perfect. If i dont use this
> synchronized(){...} block,
> it does'nt work at all ,(gives wrong answer)..
The synchronized keyword help to ensure one thread
running at a time of the given code. But it does not
lock out all other synchronized methods running in the
same object nor give you much communication between
threads.
> If I have solved the problem and its working fine
> now, WHY DO I NEED
> TO CALL wait ON some object or something
wait() and notify() and notifyAll() help the threads
to communicate with each others.
> .. And if i
> do, where in the
> following code do i need to use them
Your program does not required for it yet. =)
The sequence is notifyAll() or notify() before wait().
You already used join() - causes the compiler to wait
for the thread to die out. You could have used wait()
or sleep(), etc... to make your logic more
complicated. =)
Try to follow the thread life cycle schema, you can go
no wrong. I think Deitel & Deitel's Java How to
Program has the best Thread Life Cycle Schema.
I think your program is not finished or it is just a
part of a multiple programs.