Search:

Type: Posts; User: mds1256

Search: Search took 0.11 seconds.

  1. Replies
    15
    Views
    1,587

    [SOLVED] Re: Help with SYNCHRONIZED

    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...
  2. Replies
    15
    Views
    1,587

    [SOLVED] Re: Help with SYNCHRONIZED

    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...
  3. Replies
    15
    Views
    1,587

    [SOLVED] Re: Help with SYNCHRONIZED

    isnt that just tricking it though, in reality it should work how its programmed so I still have something wrong
  4. Replies
    15
    Views
    1,587

    [SOLVED] Re: Help with SYNCHRONIZED

    so really the synchronized method still isnt doing anything as I have removed this and I am still getting the correct output.
  5. Replies
    15
    Views
    1,587

    [SOLVED] Re: Help with SYNCHRONIZED

    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...
  6. Replies
    15
    Views
    1,587

    [SOLVED] Re: Help with SYNCHRONIZED

    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...
  7. Replies
    15
    Views
    1,587

    [SOLVED] Re: Help with SYNCHRONIZED

    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()...
  8. Replies
    15
    Views
    1,587

    [SOLVED] Help with SYNCHRONIZED

    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...
Results 1 to 8 of 8