aspose file tools *
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Question" Watch "Thread Question" New topic
Author

Thread Question

Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 219

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 ]

blog: http://tunatore.wordpress.com
SCJP 6.0 + SCWCD 1.5
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14445
    
  23

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 219

Thank you for answers , I got it

 
 
subject: Thread Question