Page 2 of 2 FirstFirst 12
Results 21 to 31 of 31
Like Tree2Likes

Thread: Threads - object visible to more than one thread

  1. #21
    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 ...
    I just considered otherwise and tested the code by adding a line to print in t, and realized that it doesn't end. So the program doesn't end. Am I wrong?

    Java Code:
    public class T implements Runnable {
     
        private int x= 42;
     
        public void run() {
             
            while (true) {
            	x= x++;
            	System.out.println("I'm still running.");
            } // 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?");
             
            
        }
    }

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

    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.

    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
    Thank you for this very important piece of information. I just searched a bit and found that since x=x++ involves a postfix increment, so it is equal to

    Java Code:
    int temp=x;
    x=x+1;
    x=temp;
    And I concluded that whenever I need to use the incremented value of x after the operation, I'll try to use a prefix increment. Thank you.

  3. #23
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,334
    Blog Entries
    7
    Rep Power
    20

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

    Quote Originally Posted by Zarah View Post
    I just considered otherwise and tested the code by adding a line to print in t, and realized that it doesn't end. So the program doesn't end. Am I wrong?
    Erm, no, you're right; it's just the while loop at line #15 that ends, so 'huh?' is printed in main( ... ); the other thread happily goes on and on ... the other thread should've been a daemon thread.

    kind regards,

    Jos (<-- shouldn't post quick examples ;-)
    cenosillicaphobia: the fear for an empty beer glass

  4. #24
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,645
    Rep Power
    5

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

    Quote Originally Posted by JosAH View Post

    Jos (<-- shouldn't post quick examples ;-)
    I argue that this thread proves it is actually a good thing to oops-up with quick examples; it leads to discussion, deeper understanding, budding love affairs and the overall betterment of mankind.

    So no, don't stop.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #25
    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 gimbal2 View Post
    budding love affairs
    Would you like a room? ;-)

  6. #26
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,645
    Rep Power
    5

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

    Are you offering?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #27
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,334
    Blog Entries
    7
    Rep Power
    20

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

    Quote Originally Posted by gimbal2 View Post
    I argue that this thread proves it is actually a good thing to oops-up with quick examples; it leads to discussion, deeper understanding, budding love affairs and the overall betterment of mankind.
    ... and the liquor store manager arriving in time with my much needed Grolsch refill ...
    cenosillicaphobia: the fear for an empty beer glass

  8. #28
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,645
    Rep Power
    5

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

    I don't see how making quick examples causes your liquor store delivery guy to arrive on time. Please explain.

    PS: even if I don't purposely choose to misunderstand your post, you contradict yourself. It would make more sense if you need your Nespresso delivery guy to be on time.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #29
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,334
    Blog Entries
    7
    Rep Power
    20

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

    Quote Originally Posted by gimbal2 View Post
    I don't see how making quick examples causes your liquor store delivery guy to arrive on time. Please explain.
    There are more things in heaven and earth friend Gimbalio ...

    Quote Originally Posted by gimbal2 View Post
    PS: even if I don't purposely choose to misunderstand your post, you contradict yourself. It would make more sense if you need your Nespresso delivery guy to be on time.
    Nespresso is yucky; I only drink Illy (the real espresso); you barbarian!
    '
    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #30
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,645
    Rep Power
    5

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

    Aha, I've upgraded from acquaintance to friend.

    The Nespresso reference was an insult of course, I couldn't think of a relevant Monty Python video.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

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

    @JosAH, OK thank you, and it was a very good example, I was just confused because I was stuck at the point that infinite while loops can probably break without a break or return statement etc. So thanks.

Page 2 of 2 FirstFirst 12

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
  •