• Post Reply Bookmark Topic Watch Topic
  • New Topic

threads  RSS feed

 
Sanjiv Saha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class test extends Thread
{ public static int i=0;
public static void main(String args[])
{ test t= new test();
t.start();
System.out.println(i);
}
public void run()
{ i=i+1;
}
}

My doubt is when i is why it is 0?
Should be 1? Please explain
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the code you have posted there is no clear sequential relationship between the code that increments 'i' and the code that prints it out. There is no happens-before relationship between the two statements.

It is likely that the thread does not get time to even start before the print statement runs, but it is also possible that the thread runs to completion but the 'main' thread does not see any changes that the second thread makes.

Note that when you call start() on the thread that method call does not block until the run() method completes, it returns as soon as it can.
 
Sanjiv Saha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I executed this..if I print i in run() it gives 1 but the print statement in main() prints 0..so I'm not really sure about the reason
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason is that you have two different threads. The statement that increments 'i' does not have a happens-before relationship with the statement that prints 'i'. That means that the statement that prints 'i' is not guaranteed to see the result of the increment.

Why do you expect it to print 1?
 
Sanjiv Saha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because I was thinking that like there is only one copy of static variable..when the class loads and different objects have access to it..so I thought that similar thing hhappens in threading..so if I am right then every thread has its own ccopy of variables which might include static variables in their ccallstack?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is only one variable i, because it is static. Threads do not have their own copy of static variables.

But there are two threads that are running independently of each other, there is no guarantee to the timing between the threads. You can't be sure that one thread has incremented the variable before the other one prints it, or the other way around.

The output of your program is unpredictable. If you run it multiple times, then it might sometimes print 0 and other times 1.
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please always indent your code; we have some suggestions about how to do it here. And also always use the code button. Unindented code is very difficult to read and is very easy to introduce mistakes into. I shall try formatting your code with code tags and you can see how much better it looks.
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what the code would look like correctly formattedThe class should be called Test not test and the main method should have the [] before args, as recommended in the Java® Language Specification.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!