This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Threads and infinite loop

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ashish and Ram,
Kindly refer to http://www.javaranch.com/ubb/Forum27/HTML/000131.html
Thanks for the replies.
Regards.
Kezia.
 
Your mother is a hamster and your father smells of tiny ads!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic