• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Implementing Runnable

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this code outputs a sequence of 5, 10, 15.. but if i change the value at i%5 to i%6 it will wtill output 5 but in a sequence of +6 like 5, 11, 17, 23, 29 ...
my problem is how come it started with 5 as the output and why, since 5 is not intialized to int i anywhere in the code and why does it follow that sequence, even when i change the value of i%5 to i%4, it still follows the same sequence in i%6. why?
from k&b
page 6

import java.lang.Runnable;

class Wombat implements Runnable {
private int i;
private int x;
public synchronized void run() {
if (i%4 != 0) { i++; }
for(int x=0; x<5; x++, i++)
{ if (x > 1) Thread.yield(); }
System.out.print(i + " ");
}
public static void main(String[] args) {
Wombat n = new Wombat();
for(int x=100; x>0; --x) { new Thread(n).start(); }
} }
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
By Default i is getting default value 0
therefore
(i%4) or (i%5) or (i% 6) will give value 0
((i%4)!=0) will give false therefore I will not get incremented and cursor move to for loop
after for loop value of i and x is 5
therefore (i%5) will give zero and i will not increment
but (i%4) and (i%6) will give remainder 1 and 5 so i will get increment
 
Remy coolcoin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you Gala for the reply.
Is my pleasure to hear from you.

My Regards
reply
    Bookmark Topic Watch Topic
  • New Topic