understanding code!!

i have this code and am finding it hard to understand it, i know it will create threads, but does it go a,b,c,a,b,c?

class StringCyclerimplements Runnable

{

private String s;

public StringCycler(String s)

{

this.s = s;

}

privatevoid printChar(int i)

{

System.out.println(this.s.charAt(i));

}

privatevoid cycle()

{

for (int i = 0; i < this.s.length(); i++)

{

this.printChar(i);

}

}

publicvoid run()

{

for (;;)

{

this.cycle();

}

}

}

publicclass Main(String[] args)

{

publicstaticvoid main(String[] args)

{

StringCycler c =new StringCycler("abc");

Thread t1 =new Thread(c);

Thread t2 =new Thread(c);

t1.start();

t2.start();

}

}

[2085 byte] By [da_master_of_nuthina] at [2007-11-26 18:10:39]
# 1

Each StringCycler instance will cycle thru a, b, and c over and over again. Since there are 2 threads doing that though, the order of execution is indeterminate. You may get a, b, a, c, b, ... depending on which thread is given CPU cycles at the time. The bolded items are from the 2nd thread for empasis.

warnerjaa at 2007-7-9 5:43:02 > top of Java-index,Java Essentials,New To Java...
# 2
> i have this code and am finding it hard to understand> it, i know it will create threads, but does it go> a,b,c,a,b,c?Probably not. ~
yawmarka at 2007-7-9 5:43:02 > top of Java-index,Java Essentials,New To Java...
# 3
how could i modify the code so it will only give me a,b,c,a,b,c?
da_master_of_nuthina at 2007-7-9 5:43:02 > top of Java-index,Java Essentials,New To Java...
# 4
> how could i modify the code so it will only give me> a,b,c,a,b,c?Don't use threads?
mlka at 2007-7-9 5:43:02 > top of Java-index,Java Essentials,New To Java...
# 5
> > how could i modify the code so it will only give me> > a,b,c,a,b,c?> > Don't use threads?Indeed. That, or develop some complex means of observation/notification. Don't rely on the thread scheduler for behavior.~
yawmarka at 2007-7-9 5:43:02 > top of Java-index,Java Essentials,New To Java...
# 6
> how could i modify the code so it will only give me> a,b,c,a,b,c?System.out.println("abcabc");Or if you use just one thread from the code you posted, it'll print "abc" over and over again.Message was edited by: paulcw
paulcwa at 2007-7-9 5:43:02 > top of Java-index,Java Essentials,New To Java...
# 7
> > how could i modify the code so it will only give me> > a,b,c,a,b,c?> > System.out.println("abcabc");<cough>commas</cough>;o)~
yawmarka at 2007-7-9 5:43:02 > top of Java-index,Java Essentials,New To Java...
# 8
If the Op is testing threads, surely the best solution is to synchronize the method cycle.private void synchronized cycle()
TimRyanNZa at 2007-7-9 5:43:02 > top of Java-index,Java Essentials,New To Java...
# 9
But even that (synchronization) isn't necessary, given the problem description. If he wants to repeat the same thing over and over again, and not interleaved, just cycle the task in a single thread.
paulcwa at 2007-7-9 5:43:02 > top of Java-index,Java Essentials,New To Java...
# 10
> If the Op is testing threads, surely the best> solution is to synchronize the method cycle.This would clearly show different levels of granularity possible within multi-threading dispatch scenarios which to my eyes appeared to be what the Op was testing.
TimRyanNZa at 2007-7-9 5:43:02 > top of Java-index,Java Essentials,New To Java...