aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads and infinite loop" Watch "Threads and infinite loop" New topic
Author

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?
Kezia.
Xavier Casals
Greenhorn

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.
Regards.
Kezis.
Ashi Rastogi
Greenhorn

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);
th1.start();th2.start();
}
}// End of TestThread
class Thread1 implements Runnable
{
public void run()
{
int i=0;
while(true)
{
System.out.println("Value of i = " + i++);
if ( i == 10)
break;
}
}
}

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

Joined: Aug 26, 2001
Posts: 19
Hello,
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...
bye,
ram..
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.
Regards.
Kezia.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads and infinite loop