Page 1 of 2 12 LastLast
Results 1 to 20 of 31
Like Tree2Likes

Thread: Threads - object visible to more than one thread

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default Threads - object visible to more than one thread

    I need a small example of an object which is visible to two different threads.

    Does saying "an object is visible to two threads" mean that it's fields or methods are being used in both the threads? I am not clear about that.

    Thank you in advance.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,539
    Rep Power
    5

    Default Re: Threads - object visible to more than one thread

    Have you every done anything with painting or Swing events? When you process a Swing event, the objects from your enclosing class (aka instance fields like String, JLabel, etc) are accessed from the Event Dispatch Thread. Those same objects are also accessible from the main thread. Thus, two different threads, same objects.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,907
    Rep Power
    5

    Default Re: Threads - object visible to more than one thread

    Quote Originally Posted by Zarah View Post
    Does saying "an object is visible to two threads" mean that it's fields or methods are being used in both the threads?
    Basically yes, although it is more correct to say that the object can -possibly- be used in both threads. It won't actually be used until you write code that uses it of course :)

    I'll paint an example.

    Java Code:
    public class Person {
       
      private String name;
      private Address address;
    
      public Person(String name, Address address){
        this.name = name;
        this.address = address;
      }
    
      public String getName(){
        return name;
      }
    
      public Address getAddress(){
        return address;
      }
    }
    Java Code:
    public class SomeThread extends Thread {   // people may attempt to lynch me for extending Thread, but beware that I flip you the finger if you do :)
    
      private Person person;
     
      public SomeThread(Person person){
         this.person = person;
      }
    
      public void run(){
        System.out.println("Name: " + person.getName());
      }
     
    }
    Please forgive any mistakes, I typed this from memory.

    Now, as you can see the Person class contains an object of type Address. Now even though that Address object in the Person is visible to the SomeThread thread, it doesn't actually ever use it in any way. So it is visible, but not used.
    Zarah likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Threads - object visible to more than one thread

    The example of gimbal2 is quite nice.

    Just imagine, that you could create multiple Threads with a reference to one Person object.
    So you can give references to threads and these objects could contain references to other objects and so on.

    But there is also one big area: The class instances are shared. So all threads access the same classes and with these classes the static elements (So you have something like System.out which is shared between threads or the singleton pattern.

    With kind regards,

    Konrad
    Zarah likes this.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Threads - object visible to more than one thread

    The following example is more sicko: there are two threads: one continuously sets a variable x to the value 42 while the other one checks if that variable equals 42; and still the program ends ...

    Java Code:
    public class T implements Runnable {
    
    	private int x= 42;
    
    	public void run() {
    		
    		while (true) x= x++; // set x to 42
    	}
    	
    	private void check() {
    		
    		while (x == 42); // check if x equals 42
    	}
    	
    	public static void main(String[] args) {
    
    		T t= new T();
    		
    		new Thread(t).start();
    		
    		t.check();
    		
    		System.out.println("huh?");
    	}
    }
    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,907
    Rep Power
    5

    Default Re: Threads - object visible to more than one thread

    I have that nagging feeling you actually meant to post that in the other thread on threads.

    What are interleaving operations? Can atomic actions interleave?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default Re: Threads - object visible to more than one thread

    Have you every done anything with painting or Swing events? When you process a Swing event, the objects from your enclosing class (aka instance fields like String, JLabel, etc) are accessed from the Event Dispatch Thread. Those same objects are also accessible from the main thread. Thus, two different threads, same objects.
    Not really, but this has given me an idea. Thank you.

  8. #8
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default Re: Threads - object visible to more than one thread

    @ gimbal2, Thank you very much. This was a very clear example.

    @Konrad, Thank you, the example was very nice.
    Last edited by Zarah; 03-24-2014 at 08:25 PM.

  9. #9
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default Re: Threads - object visible to more than one thread

    and still the program ends ...
    Thank you. If you can elaborate why it ends because I haven't been able to understand that?

    I apologize for an untimely response, I could not get it and I was just waiting for my brain to function better.

  10. #10
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Threads - object visible to more than one thread

    Hi,

    the important thing is, to understand the "x = x++". This is some crazy command and whoever writes something like that should simply be slapped a little bit.

    x is increased but the old value is stored in x afterwards.

    So x is only increased for a very very short time. But when the application runs a little bit, the second thread will see x while it is still incremented.

    I am sure that there are a lot of discussions on x=x++ on the net.

    With kind regards,

    Konrad

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,907
    Rep Power
    5

    Default Re: Threads - object visible to more than one thread

    Quote Originally Posted by kneitzel View Post
    Hi,

    the important thing is, to understand the "x = x++". This is some crazy command and whoever writes something like that should simply be slapped a little bit.
    Java Code:
    x = x++;
    Well? What are you waiting for? On the left butt-cheek please.

    While I'm off-topic anyway, I have a compelling reason to request the merging of subforums. Less subforums = less spam, because as the recent mass spammer proves, a tactic is to add a spam post to each and every subforum...

    I know, I'm naive.
    Last edited by gimbal2; 03-27-2014 at 10:23 AM. Reason: engrish!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Threads - object visible to more than one thread

    Quote Originally Posted by gimbal2 View Post
    Java Code:
    x = x++;
    Well? What are you waiting for? On the left butt-cheek please.

    While I'm off-topic anyway, I have a compelling reason to request the merging of subforums. Less subforums = less spam, because as the reason mass spammer proves, a tactic is to add a spam post to each and every subforum...

    I know, I'm naive.
    Don't under estimate the powers of the moderators and their rotating knives machine; this morning alone I sliced up numerous spammers again ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Threads - object visible to more than one thread

    Hi gimbal2,

    your wish is my command - or what do I have to say?

    *slap* *slap* :)

    I have a C / C++ background and if I remember correctly, the X = X++; can have different results on different compilers (so the result is undefined).

    But I loved Jos example. Was quite nice to see and it and think about it. But I doubt that you would consider it a good coding practice :)

    With kind regards,

    Konrad

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,907
    Rep Power
    5

    Default Re: Threads - object visible to more than one thread

    Quote Originally Posted by JosAH View Post
    Don't under estimate the powers of the moderators and their rotating knives machine; this morning alone I sliced up numerous spammers again ...

    kind regards,

    Jos

    Unfortunately that machine does not work when you are doing whatever job you do. You're a fish tank cleaner right?

    EDIT:

    fish, slapping, yep this thread is now calling for it:

    https://www.youtube.com/watch?v=xCwLirQS2-o
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Threads - object visible to more than one thread

    Quote Originally Posted by gimbal2 View Post
    Unfortunately that machine does not work when you are doing whatever job you do. You're a fish tank cleaner right?
    Define "does not work"; did I overlook a spammer?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,907
    Rep Power
    5

    Default Re: Threads - object visible to more than one thread

    No, but now you're not cleaning fish tanks.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Threads - object visible to more than one thread

    Quote Originally Posted by gimbal2 View Post
    No, but now you're not cleaning fish tanks.
    How many times do I have to tell you I've been busy slicing up spammers; here, watch: Monty Python-The Black Knight - YouTube

    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,907
    Rep Power
    5

    Default Re: Threads - object visible to more than one thread

    Its only a flesh wound!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Threads - object visible to more than one thread

    Quote Originally Posted by gimbal2 View Post
    Its only a flesh wound!
    Don't talk nonsense: it was a hell of a job to get those rotating knives installed: Monty Python- Architect Sketch - YouTube

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Threads - object visible to more than one thread

    Quote Originally Posted by kneitzel View Post
    I have a C / C++ background and if I remember correctly, the X = X++; can have different results on different compilers (so the result is undefined).
    Actually, for java, the result IS defined and should be consistent across all virtual machines and compiler implementations. For c/c++ you are correct though.

Page 1 of 2 12 LastLast

Similar Threads

  1. object not visible
    By jim01 in forum New To Java
    Replies: 4
    Last Post: 09-05-2011, 01:43 AM
  2. Creating new threads from another thread
    By enzom83 in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-03-2010, 10:15 PM
  3. Replies: 3
    Last Post: 10-20-2010, 06:24 AM
  4. how to wrk with twa threads then compile both to 1 thread
    By dmotah in forum Threads and Synchronization
    Replies: 0
    Last Post: 02-04-2008, 09:53 AM
  5. Replies: 0
    Last Post: 01-28-2008, 07:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •