Dear, I do have a question regarding ProducerConsumerSynchronized example during Java SE Thread (1021) session. When synchronized keyword had been introduced in put & get methods everything works fine but I tried to remove sleep method sleep((int)(Math.random() * 100)); - (line 16) from producer's run method since I believe the flag within CubbyHole class is more than enough to control the content/value access. When I test the result I got the same behavior except that producer sometime putted more than one value but the good thing here consumer will not skip the value before consuming the previous one. For instance:
Producer #1 put: 0 Producer #1 put: 1 Consumer #1 got: 0 Consumer #1 got: 1 Producer #1 put: 2 Consumer #1 got: 2 Producer #1 put: 3 Consumer #1 got: 3 Producer #1 put: 4 Consumer #1 got: 4 Producer #1 put: 5 Consumer #1 got: 5 Producer #1 put: 6 Consumer #1 got: 6 Producer #1 put: 7 Consumer #1 got: 7 Producer #1 put: 8 Producer #1 put: 9 Consumer #1 got: 8 Consumer #1 got: 9 However, when I wrapped cubbyhole object using synchronized statement (common object), the results returned to normal where producer can only put one value at a time & constrains it action till the moment where the consumer change the flag value. My question here: how did the producer manage to put more than one value in the previous example (highlighted in red) when we only removed the sleep method from producer's run method - (line 16)? regards, Khaled -- You received this message because you are subscribed to the Google Groups "JPassion.com: Java Programming" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. Visit this group at http://groups.google.com/group/jpassion_java. For more options, visit https://groups.google.com/groups/opt_out.
