Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: how to assign different task to different threads

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to assign different task to different threads

    Please go through this code and help me to achieve the desired output

    import java.util.Date;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import cyclicbarrier.Hashmap;
    public class ThreadpoolDemo {
    private static final int nthreads=10;
    public static void main(String[] args) throws InterruptedException {
    ExecutorService exe=Executors.newFixedThreadPool(nthreads);
    for(int i=1;i<7;i++)
    {
    Runnable worker=new Myrunnable(i);
    exe.execute(worker);
    }
    exe.shutdown();
    }
    }
    class Myrunnable implements Runnable
    {
    private int ii;
    Myrunnable(int i)
    {
    ii=i;
    }
    public void run()
    {
    Hashmap hsamap=new Hashmap();
    System.out.println(hsamap.getcity(ii));
    System.out.println(Thread.currentThread().getName( )+" Executed this");
    Thread.currentThread().stop();
    System.out.println(new Date());
    }
    }

    import java.util.HashMap;
    public class Hashmap {
    public String getcity(int i)
    {
    HashMap<Integer,String> hm=new HashMap<Integer,String>();
    hm.put(1, "AAAA");
    hm.put(2, "BBBB");
    hm.put(3, "CCCC");
    hm.put(4, "DDDD");
    hm.put(5, "EEEE");
    hm.put(6, "FFFF");
    String value=hm.get(i);
    return value;
    }
    }

    Output:
    BBBB
    pool-1-thread-2 Executed this
    DDDD
    pool-1-thread-4 Executed this
    FFFF
    pool-1-thread-6 Executed this
    AAAA
    pool-1-thread-1 Executed this
    CCCC
    pool-1-thread-3 Executed this
    EEEE
    pool-1-thread-5 Executed this

    now if I change the values to for(int i=3;i<7;i++),I should get the output as
    DDDD
    pool-1-thread-4 Executed this
    EEEE
    pool-1-thread-5 Executed this
    FFFF
    pool-1-thread-6 Executed this
    means Thread 1 should always display "AAAA" if i request,Thread 2 should always display "BBBB" and so on Please tell me how can i achieve this. or else if I assign i=3; then thread 3 only should display corresponding Hashmap values,if I assign i=5;then thread 5 only should display corresponding Hashmap values. Thanks in advance


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: how to assign different task to different threads

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Can you add some comments to the output describing what you want it to look like. For example:
    BBBB <<<<<<<<<<<<<THIS SHOULD BE AAAA
    pool-1-thread-2 Executed this
    DDDD
    pool-1-thread-4 Executed this
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Task in java with Threads
    By deitynitros in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 27th, 2013, 04:10 AM
  2. Assign layout_width programmatically
    By ptia in forum Android Development
    Replies: 2
    Last Post: December 16th, 2012, 04:09 AM
  3. Assign a value in a void method
    By Freezer Boy in forum Object Oriented Programming
    Replies: 4
    Last Post: December 12th, 2012, 11:13 AM
  4. Cannot assign requested address
    By frozen java in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 12th, 2012, 01:32 PM
  5. How to assign values from a file to an arraylist of objects?
    By nadman123 in forum Collections and Generics
    Replies: 3
    Last Post: September 15th, 2008, 01:07 PM

Tags for this Thread