Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Threads
Search Coderanch
Advance search
Google search
Register / Login
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
Forum:
Threads and Synchronization
Callable collection methods
meeta gaur
Ranch Hand
Posts: 305
I like...
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.util.concurrent.Callable; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; import java.util.HashSet; import java.util.Set; class CallableCollection implements Callable { Integer i=0; public Integer call(){ return ++i; } public static void main(String[] args) { CallableCollection obj=new CallableCollection(); Set s=new HashSet(); s.add(obj); s.add(obj); s.add(obj); s.add(obj); s.add(obj); s.add(obj); Integer x=0; ExecutorService e=Executors.newFixedThreadPool(2); try{ x=(Integer)e.invokeAny(s); } catch(Exception ee){ System.out.println(ee); } System.out.println(x); e.shutdown(); } }
1
Why does it always choose output 1 ?
My one regret in life is that I am not someone else.
- Woody Allen
Matthew Brown
Bartender
Posts: 4568
9
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You're adding the same
CallableCollection
6times to a
HashSet
. The last 5 times have no effect, because the set won't allow duplicates.
Also, the first call to call for any
CallableCollection
will return 1. And you only call it once on any
CallableCollection
.
meeta gaur
Ranch Hand
Posts: 305
I like...
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.util.concurrent.Callable; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; import java.util.HashSet; import java.util.Set; class CallableCollection implements Callable { String name; CallableCollection(String name){ this.name=name; } public String call(){ return this.name; } public static void main(String[] args) { CallableCollection obj1=new CallableCollection("one"); CallableCollection obj2=new CallableCollection("two"); CallableCollection obj3=new CallableCollection("three"); CallableCollection obj4=new CallableCollection("four"); CallableCollection obj5=new CallableCollection("five"); CallableCollection obj6=new CallableCollection("six"); CallableCollection obj7=new CallableCollection("seven"); Set s=new HashSet(); s.add(obj1); s.add(obj2); s.add(obj3); s.add(obj4); s.add(obj5); s.add(obj6); s.add(obj7); String output; ExecutorService e=Executors.newFixedThreadPool(2); try{ output=(String)e.invokeAny(s); System.out.println(output); } catch(Exception ee){ System.out.println(ee); } e.shutdown(); } }
six
Thanks, i made some changes, now it gives me different output each time.
My one regret in life is that I am not someone else.
- Woody Allen
meeta gaur
Ranch Hand
Posts: 305
I like...
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
My one regret in life is that I am not someone else.
- Woody Allen
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Errata ?
Generic
using Sets
Set question
for loop
More...