Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Question

 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Taken from Enthuware Test Studio

package com.sjcp.ThreadsExamples;

public class EnthuwareThread extends Thread{
public static int threadcounter = 0 ;
public void run()
{
threadcounter++;
System.out.println(threadcounter);

}
public static void main(String[] args) {

for(int i=0; i<1000; i++) //huge pool
{
synchronized(EnthuwareThread.class)
{
new EnthuwareThread().start();
}
}


}

}

When I run this code, it works fine and increase the counter with order,

1
2
3
4
5
6
7
8
9
10
11
12
13
.
.
998
999
1000

however in comment section it says that

Here, the synchronized(TestClass.class) has no impact because the syncronized block in not using any shared variable. So, the main method creates 1000 threads and each thread increments the threadcounter once. Therefore, the final value of threadcounter will be 1000.
The printing of the values cannot be determined because the run() method is not synchronized. So, while a thread has incremented the threadcounter and is about to print it, another thread may increment the value.


But I see in the console, all variables ordered ?? I am confused with the comment. ?? Can ideas ? Why the result is ordered and not changed when I run the program at every time.


[ October 18, 2008: Message edited by: Anut Walidera ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15484
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In principle the order in which the threads run is not predictable. It might be that if you run this on a different operating system, the order is different. Just because the numbers are neatly ordered on your system doesn't mean that there's a predictable order.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anut Walidera:
...while a thread has incremented the threadcounter and is about to print it, another thread may increment the value...

In theory, this could happen, but it's not very likely.

To make it more likely (but still not guaranteed) that the thread scheduler will switch threads at that particular point in the code, you can add a short call to sleep (with the appropriate try/catch block)...

As Jesper pointed out, thread behavior is OS dependent. So if the sleep argument of 1 doesn't make a difference on your machine, increase it until you see a difference. (If you're running Windows, you might need to give it a relatively large value.)
[ October 18, 2008: Message edited by: marc weber ]
 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for answers , I got it

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic