1 2 Previous Next 20 Replies Latest reply: Sep 12, 2006 6:09 PM by 796440 Go to original post RSS
      • 15. Re: Problems with synchronizing an element in an array
        807569
        Please read replies 1 and 7 and explain what parts of those solution that you are not understanding. I don't need to see your requirements any more. I understand them just fine. You don't seem to be understanding the workable solutions offered.
        • 16. Re: Problems with synchronizing an element in an array
          796440
          Also read my replies. I haven't paid as much attention to your requirements as cotton, but the principles I described are still relevant.
          • 17. Re: Problems with synchronizing an element in an array
            807569
            Also read my replies. I haven't paid as much
            attention to your requirements as cotton, but the
            principles I described are still relevant.
            Yes you should read jverd's replies too. He is taking the time to explain to you what is happening in more detail. I am impatient and just trying to address your current problem directly.

            So you can go either way (or both). Read and understand jverd's replies so that you can figure it out on your own or implement what I said and
            figure out why it works later.

            Although besides impatience I also find that when someone has confusion about how something works I find that giving a solution that does work can help because sometimes such persons are more likely to believe what you tell them.
            • 18. Re: Problems with synchronizing an element in an array
              807569
              well as far as I understand there is no workaround, I need to synchronize when modifying shared data. But all other synchronized methods will be blocked while a single thread executes one of these synchronized methods. Therefore only a single thread can use the array at a time and all the others will block until its finished. This might not be visible to the eye in practice but in theory it a bottleneck.

              I am not very experienced with thread safe programs so thanks for your patience so far and I think the best way to get a better understanding would be to find some good litterature since I lack the basic knowledge.
              • 19. Re: Problems with synchronizing an element in an array
                807569
                well as far as I understand there is no workaround,
                Correct. However a better design can make this moot.
                I
                need to synchronize when modifying shared data.
                Correct. Sort of.
                But
                all other synchronized methods will be blocked while
                a single thread executes
                No.
                one of these synchronized
                methods. Therefore only a single thread can use the
                array at a time and all the others will block until
                its finished.
                No.
                This might not be visible to the eye in
                practice but in theory it a bottleneck.
                If the rest was correct then yes.
                I am not very experienced with thread safe programs
                so thanks for your patience so far and I think the
                best way to get a better understanding would be to
                find some good litterature since I lack the basic
                knowledge.
                Think of it this way you want to spend the least time in the synchronized block as possible right?

                So you need to either remove the object you want to modify from the shared list OR taint it so that other threads know that it is locked from delettion.

                As soon as that part is done (which wouldn't take very long) you can release your synchronized lock and the next thread can access the shared data.

                etc.
                • 20. Re: Problems with synchronizing an element in an array
                  796440
                  well as far as I understand there is no workaround, I
                  need to synchronize when modifying shared data. But
                  all other synchronized methods will be blocked while
                  a single thread executes one of these synchronized
                  methods. Therefore only a single thread can use the
                  array at a time and all the others will block until
                  its finished. This might not be visible to the eye in
                  practice but in theory it a bottleneck.
                  Look, syncing is used all over Java, other programs, OSes, etc. The idea is to keep the sync blocks as small as possible. If it still causes problems, then you have to go with more messy, complex solutions with multiple copies of things, sycning them up in low priority threads, etc. etc. Don't worry about that now. First worry about making it correct.

                  Syncing to avoid corrupting shared data is a fact of life. If you do it right, it doesn't usually cause performance problems.


                  I am not very experienced with thread safe programs
                  so thanks for your patience so far and I think the
                  best way to get a better understanding would be to
                  find some good litterature since I lack the basic
                  knowledge.
                  Indeed. And write some code and play with it, rather than just worrying about what kinds of convoluted machinations you can perform to get around performance problems you're guessing might happen.

                  "It's easier to optimize a correct program than to correct an optimized program."
                  --Somebody


                  Sun's multithreading tutorial:
                  http://java.sun.com/docs/books/tutorial/essential/concurrency/

                  Also look for the book Concurrent Programming in Java by Doug Lea.
                  1 2 Previous Next