The moose likes Threads and Synchronization and the fly likes Threads and infinite loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads and infinite loop" Watch "Threads and infinite loop" New topic

Threads and infinite loop

Kezia Matthews
Ranch Hand

Joined: May 19, 2001
Posts: 107

I had posted a question on August 25, 2001 on the topic "Note on thread" started by Mr. Mukesh Rathod on October 19, 2000. I have not received any reply till today, i.e, August 28, 2001. I am not sure, if I can post a query on a topic almost a year old. Anyay, since I've not received any reply for my question, I am posting it as a new topic.
The following code results in an infinite loop.

(i) Can anybody tell me whether I am missing something? Why does the 'while' loop not exit when 'i' becomes equal to 10?
(ii) I am using Win98. Is this a pre-emptive or time-sliced system? Has this got something to do with the above mentioned behaviour?
Xavier Casals

Joined: Aug 16, 2001
Posts: 4
I suppose that it happens because the two threads can access the variable 'i' at the same time. And the variable 'i' can be incremented twice before reading the line where you compare the value of 'i' with 10. You had to use a mechanism to prevent it (the line with i++ has to be executed like an atom with the comparison).
If there was only one thread, it has to work because there is only one access at the same time.
Try to use > instead of ==, and I am sure that it will exit from the loop!
I hope it helps you.
Kezia Matthews
Ranch Hand

Joined: May 19, 2001
Posts: 107
Hi Xavier,
What you are saying could be correct. One thread after printing 'i=9' increments the value in 'i', checks for the condition 'i==10' and exits the 'while' loop. The other thread, prints 'i=10' and increments the value of 'i' and after this checks the condition 'i==10'. Since the value of 'i' now would be 11, the second thread goes into an infinite loop. If the condition 'i==10'is put before incrementing it, the infinite loop can be avoided. The run method can be modified as below:

This is clear to me now.
I hope somebody answers my second question, whether Win98 is pre-emptive or time-sliced system.
Ashi Rastogi

Joined: Aug 30, 2001
Posts: 2
U check out this code ....i suppose this wud be the answer for uor a year long query...
class TestThread
public static void main(String args [])
Thread1 t1 = new Thread1();
Thread th1 = new Thread(t1);
Thread th2 = new Thread(t1);
}// End of TestThread
class Thread1 implements Runnable
public void run()
int i=0;
System.out.println("Value of i = " + i++);
if ( i == 10)

The vary reason behind putting uor loop in to infinite one as u have defined uor variable i out side of the run method .
Ramalingam Vijayakumar

Joined: Aug 26, 2001
Posts: 19
The tried out your code in my m/c ..Its works perfectly...
The concept of your program is 2 threads sharing the SAME DATA AND CODE....The problem can be eliminated when you make your run method synchronized..
public synchronized void run() { }
hope you know the concept behind this(using synchronization..)
Windows98 folllows preemptive scheduling...
Kezia Matthews
Ranch Hand

Joined: May 19, 2001
Posts: 107
Hello Ashish and Ram,
Kindly refer to http://www.javaranch.com/ubb/Forum27/HTML/000131.html
Thanks for the replies.
I agree. Here's the link: http://aspose.com/file-tools
subject: Threads and infinite loop
Similar Threads
static synchronized
Concurrent threads
Note on Thread
Regarding Thread's join()
How to interrupt or stop the threads