File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Thread Question

Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 220

Taken from Enthuware Test Studio

package com.sjcp.ThreadsExamples;

public class EnthuwareThread extends Thread{
public static int threadcounter = 0 ;
public void run()

public static void main(String[] args) {

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



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


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 ]


Core Spring Certified Professional + SCJP 6.0 + SCWCD 1.5
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15091

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

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
Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 220

Thank you for answers , I got it

I agree. Here's the link:
subject: Thread Question
jQuery in Action, 3rd edition