Weird behavior in my code.

class MyCounterimplements Runnable{

publicvoid run( ){

for (int i = 0; i <= 100; ++i ){

if ( ( i % 10) == 0 ){

Thread.yield();

System.out.println( Thread.currentThread().getName() +

" Current i = " + i );

}// if

}// for

}

}

publicclass Synch{

publicstaticvoid main( String [] args ){

MyCounter[] ms =new MyCounter[10];

for (int i = 0; i < 10; ++i ){

ms[i] =new MyCounter();

}

MyCounter mm =new MyCounter();

for (int i = 0; i < 10; ++i ){

Thread t =new Thread( ms[i], ("Thread " + i) );

t.setPriority( (i + 1) );

t.start();

}

}

}

The above code works fine, it starts each thread and the program runs to completion.

But when I replace

for (int i = 0; i < 10; ++i ){

ms[i] =new MyCounter();

}

with

for ( MyCounter m : ms )

m =new MyCounter();

Nothing happens. The run method in MyCounter is never called. I am sure there has to be an explanation for this, I just don't see it. Does anyone?

[2542 byte] By [Augiea] at [2007-10-2 16:40:47]
# 1
The enhanced for loop does not allow modification of the underlying Iterable because an Iteration does not allow this. In other words, use what works.
IanSchneidera at 2007-7-13 17:49:25 > top of Java-index,Java Essentials,Java Programming...
# 2
> > Nothing happens. The run method in MyCounter is never> called. I am sure there has to be an explanation for> this, I just don't see it. Does anyone?Where is the code where you call start? What exactly have you replaced with what?
IanSchneidera at 2007-7-13 17:49:25 > top of Java-index,Java Essentials,Java Programming...
# 3
Really!! I was not aware of this, could you please point me to some reference that I can read to verify this..Thanks.
Augiea at 2007-7-13 17:49:25 > top of Java-index,Java Essentials,Java Programming...
# 4
> Really!! I was not aware of this, could you please> point me to some reference that I can read to verify> this.. http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html
aniseeda at 2007-7-13 17:49:25 > top of Java-index,Java Essentials,Java Programming...
# 5
> Really!! I was not aware of this, could you please> point me to some reference that I can read to verify> this..> > Thanks. http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html
prometheuzza at 2007-7-13 17:49:25 > top of Java-index,Java Essentials,Java Programming...
# 6

I think this is one of those scenarios where the compiler should have said something. For instance, look at this code.

void method( List < ? extends Number > l ) {

l.add( new Integer(2) );

}

The above code will not compile because you are not supposed to make modifications to the list being passed in. This is very similar to the enhanced for-loop situation.

Augiea at 2007-7-13 17:49:25 > top of Java-index,Java Essentials,Java Programming...
# 7

> I think this is one of those scenarios where the

> compiler should have said something. For instance,

> look at this code.

>

> > void method( List < ? extends Number > l ) {

>

>l.add( new Integer(2) );

> }

>

>

> The above code will not compile because you are not

> supposed to make modifications to the list being

> passed in. This is very similar to the enhanced

> for-loop situation.

I don't have a 1.5 compiler here, but are you SURE that doesn't compile? There shouldn't be a reason that you can't add something to 'l'. The compiler can't tell you that here.

MLRona at 2007-7-13 17:49:25 > top of Java-index,Java Essentials,Java Programming...
# 8

This is a way of making generics polymorphic without actually compromising the goal of generics in the first place- type safety.

class Vehicle {}

class Truck extends Vehicle{}

class Car extends Vehicle{}

void method( List<? extends Vehicle > l ) {

l. add( new Car() );

}

The method above makes it possible to pass any kind of List<Vehicle> to the method thus enabling polymorphism with generic collections. This could be a list of Cars orTrucks but at runtime the JVM does not know which one it is.

What will prevent you from adding a Truck to a List<Car>? That is why the code will not compile.

Augiea at 2007-7-13 17:49:25 > top of Java-index,Java Essentials,Java Programming...
# 9

class Parent {}

class Child extends Parent{}

class StepChild extends Parent{}

class GrandChild extends Child {}

void method( List<? super Child > l ) {

l. add( new Child() );

}

The above method will compile and run mainly because you are saying that the method will accept any List of type Child or super class of Child. So if you add anything that is in that inheritance hierachy, it will still be fine.

Augiea at 2007-7-13 17:49:25 > top of Java-index,Java Essentials,Java Programming...