

![]() | Start a set with this search |
![]() | Include this search in one of my sets |
![]() | Exclude this search from one of my sets |
![]() | Permalink to these results Paste this link in email or IM: |
| Atom feed for tracking future search results Paste this URL into your reader: |
2 messages in org.codehaus.groovy.userRe: [groovy-user] Porting a java clas...| From | Sent On | Attachments |
|---|---|---|
| st.clair | Mar 29, 2008 1:05 pm | |
| Adam Rinehart | Apr 3, 2008 2:04 pm |

![]() | Permalink for this message Paste this link in email or IM: |
![]() | Permalink for this thread Paste this link in email or IM: |
| Atom feed for this thread Paste this URL into your reader: |
| Subject: | Re: [groovy-user] Porting a java class to groovy | Actions... |
|---|---|---|
| From: | Adam Rinehart (adam...@gmail.com) | |
| Date: | Apr 3, 2008 2:04:21 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:







