0

I'm messing around with some sample code that by default, prints an array of strings shown toward the top of the code that I pasted below. I would like to randomize how the array is displayed. I tried adding Collections.shuffle(messages); directly below where the array is declared, but it didn't work. I used the proper import, so that isn't an issue. I don't have a good grasp of how to randomize arrays, but that is the best attempt I can come up with from the research I've done. Can anyone help?

class Producer
    implements Runnable
{
    private BlockingQueue<String> drop;
    List<String> messages = Arrays.asList(
        "Mares eat oats",
        "Does eat oats",
        "Little lambs eat ivy",
        "Wouldn't you eat ivy too?");


    public Producer(BlockingQueue<String> d) { this.drop = d; }

    public void run()
    {
        try
        {
            for (String s : messages)
                drop.put(s);
            drop.put("DONE");
        }
        catch (InterruptedException intEx)
        {
            System.out.println("Interrupted! " + 
                "Last one out, turn out the lights!");
        }
    }    
}

class Consumer
    implements Runnable
{
    private BlockingQueue<String> drop;
    public Consumer(BlockingQueue<String> d) { this.drop = d; }

    public void run()
    {
        try
        {
            String msg = null;
            while (!((msg = drop.take()).equals("DONE")))
                System.out.println(msg);
        }
        catch (InterruptedException intEx)
        {
            System.out.println("Interrupted! " + 
                "Last one out, turn out the lights!");
        }
    }
}

public class SynQApp
{
    public static void main(String[] args)
    {
        BlockingQueue<String> drop = new SynchronousQueue<String>();
        (new Thread(new Producer(drop))).start();
        (new Thread(new Consumer(drop))).start();
    }
}
Bryan
  • 2,951
  • 11
  • 59
  • 101

2 Answers2

2

This should work:

public void run()
{
    Collections.shuffle(messages);//randomize array before adding into the queue

    try
    {
        for (String s : messages)
            drop.put(s);
        drop.put("DONE");
    }
    catch (InterruptedException intEx)
    {
        System.out.println("Interrupted! " + 
            "Last one out, turn out the lights!");
    }
}  
ortis
  • 2,203
  • 2
  • 15
  • 18
0

user1071777 is right, just call shuffle in the Producer's constructor.

class Producer
    implements Runnable
{
    private BlockingQueue<String> drop;
    List<String> messages = Arrays.asList(
        "Mares eat oats",
        "Does eat oats",
        "Little lambs eat ivy",
        "Wouldn't you eat ivy too?");


    public Producer(BlockingQueue<String> d) 
    { 
        this.drop = d; 
        Collections.shuffle(messages);
    }

    public void run()
    {
        try
        {
            for (String s : messages)
                drop.put(s);
            drop.put("DONE");
        }
        catch (InterruptedException intEx)
        {
            System.out.println("Interrupted! " + 
                "Last one out, turn out the lights!");
        }
    }    
}

class Consumer
    implements Runnable
{
    private BlockingQueue<String> drop;
    public Consumer(BlockingQueue<String> d) { this.drop = d; }

    public void run()
    {
        try
        {
            String msg = null;
            while (!((msg = drop.take()).equals("DONE")))
                System.out.println(msg);
        }
        catch (InterruptedException intEx)
        {
            System.out.println("Interrupted! " + 
                "Last one out, turn out the lights!");
        }
    }
}

public class SynQApp
{
    public static void main(String[] args)
    {
        BlockingQueue<String> drop = new SynchronousQueue<String>();
        (new Thread(new Producer(drop))).start();
        (new Thread(new Consumer(drop))).start();
    }
}
Dinu Sorin
  • 215
  • 2
  • 6