2 messages in org.codehaus.groovy.userRe: [groovy-user] Porting a java clas...| From | Sent On | Attachments |
|---|---|---|
| st.clair | 29 Mar 2008 13:06 | |
| Adam Rinehart | 03 Apr 2008 14:04 |
| Subject: | Re: [groovy-user] Porting a java class to groovy![]() |
|---|---|
| From: | Adam Rinehart (adam...@gmail.com) |
| Date: | 04/03/2008 02:04:44 PM |
| List: | org.codehaus.groovy.user |
This compiles and runs:
import java.util.concurrent.atomic.AtomicInteger;
public class ThreadId { // Atomic integer containing the next thread ID to be assigned private static final AtomicInteger nextId = new AtomicInteger(0);
// Thread local variable containing each thread's ID private static final ThreadLocal<Integer> threadId = [ initialValue: { return nextId.getAndIncrement(); } ] as ThreadLocal<Integer>; // The Semi-colon at the end of the prior line seems to be required.
// Returns the current thread's unique ID, assigning it if necessary public static int get() { System.out.println( "Thread ID: " + threadId.get()); return threadId.get(); }
public static void main( String[] args ) { 1.upto(3) { Thread.start { new ThreadId().get(); } } }
}
On 3/29/08, st.clair <st_c...@flowja.com> wrote:
Hello,
I have the following class:
import java.util.concurrent.atomic.AtomicInteger;
public class ThreadId { // Atomic integer containing the next thread ID to be assigned private static final AtomicInteger nextId = new AtomicInteger(0);
// Thread local variable containing each thread's ID private static final ThreadLocal<Integer> threadId = new ThreadLocal<Integer>() { @Override protected Integer initialValue() { return nextId.getAndIncrement();
} }
// Returns the current thread's unique ID, assigning it if necessary public static int get() { System.out.println( "Thread ID: " + threadId.get()); return threadId.get(); }
public static void main( String[] args ) {
new ThreadId().get();
}
}
What is the groovy equivlent of this java construct in th above class?:
// Thread local variable containing each thread's ID private static final ThreadLocal<Integer> threadId = new ThreadLocal<Integer>() { @Override protected Integer initialValue() { return nextId.getAndIncrement();
} }
I am asking because no matter what I tried, Groovy does not recoginze the method initialValue().
Thanks.
--
View this message in context:
http://www.nabble.com/Porting-a-java-class-to-groovy-tp16373926p16373926.html
Sent from the groovy - user mailing list archive at Nabble.com.
--------------------------------------------------------------------- To unsubscribe from this list, please visit:
--------------------------------------------------------------------- To unsubscribe from this list, please visit:




