Your right :)
That took a bit to sink in ha ha :)
I have now added a Thread.sleep and without the synchronized keyword all the threads return 4 as they can run that method at the same time but...
Type: Posts; User: mds1256
Your right :)
That took a bit to sink in ha ha :)
I have now added a Thread.sleep and without the synchronized keyword all the threads return 4 as they can run that method at the same time but...
Ok
I will start from the beginning, what I am trying to achieve is to have multiple threads that all have a shared resource (the NUM variable in the shared class).
I then need the threads to...
isnt that just tricking it though, in reality it should work how its programmed so I still have something wrong
so really the synchronized method still isnt doing anything as I have removed this and I am still getting the correct output.
Think I have sorted it. Took on board what you said about the resource not locking between the methods so it could be calling getNum() before actually incrementing etc.
I have changed my code so...
Yeah I understand that the OS scheduler decides which thread runs when but the reason why I want to SYNCHRONIZE is to have a shared resource that when the thread executes it will add 1 to the NUM...
Hi
I have now added the output to the original post.
It theory if all the threads called addToNum() then they call called getNum() the result would be 4 due to the 4 threads calling addToNum()...
Hi
Starting to learn about Threads and locking shared resources but I have tried this out but it doesnt seem to be working, im sure I have missed something out somewhere but cannot find it...