Re: Two more multithreading questions



Knute Johnson wrote:
I've got two specific scenarios I want to ask about:

1) I have a class with an instance variable that is a reference to a JDialog. In one thread I create new instances of JDialog and make them visible. They might get closed in this thread as well. In another thread I close the JDialog using the class instance variable. To ensure that my dialog closing thread always has a reference to the current dialog I created the instance variable with volatile. Is this adequate to guarantee that my closing thread always has a reference to the latest dialog?

I believe most javax.swing component access is supposed to be done in
the event handling thread anyway. Swing was not designed to be
multithread-safe.

2) If I want to access/modify an Object in two threads, can I use a reference to any Object in the synchronize statement to do that? It doesn't have to be the reference to the Object that I am trying to protect as long as the accesses are all in synchronized blocks?

Two threads can be in synchronized blocks at the same time, as long as
they are synchronized on different objects. You don't have to use the
object you are protecting, but all accesses to that object must be
synchronized on the same lock object.

Patricia
.



Relevant Pages

  • Re: Two more multithreading questions
    ... In one thread I create new instances of JDialog and make ... To ensure that my dialog closing thread always has a reference to the ... The safest way to insure that your Thread R only sees what its supposed to is to wrap bother the object modifying and object querying code in synchronize blocks that sync on the same object O. That object O can be ANY object. ... public void modifyFile() { ...
    (comp.lang.java.programmer)
  • Re: Two more multithreading questions
    ... Patricia Shanahan wrote: ... I have a class with an instance variable that is a reference to a JDialog. ... To ensure that my dialog closing thread always has a reference to the current dialog I created the instance variable with volatile. ...
    (comp.lang.java.programmer)
  • Re: Two more multithreading questions
    ... In one thread I create new instances of JDialog and make ... To ensure that my dialog closing thread always has a reference to the ... current dialog I created the instance variable with volatile. ... code in synchronize blocks that sync on the same object O. That ...
    (comp.lang.java.programmer)
  • Re: Two more multithreading questions
    ... In one thread I create new instances of JDialog and make them ... that my dialog closing thread always has a reference to the current ... protect as long as the accesses are all in synchronized blocks? ... The 'obj' is not protected ...
    (comp.lang.java.programmer)
  • Re: REQ: High Performance Access to a Static Objects List
    ... one reason to copy to a local variable would be so that the static object's reference could be updated by one thread without affecting the instance being used by another. ... Easier would be to synchronize all access to the list, but then even readers of the list would wind up serialized, which could hinder performance. ... If each element of the list requires extensive time to process during the iteration then it's possible copying the list could speed things up. ... However, you may still have synchronization issues between individual list elements if you do a shallow copy, or you may not find there's any significant performance benefit if you do a deep copy. ...
    (microsoft.public.dotnet.languages.csharp)