Test Twenty Threads Running

This is part of JavaUnitTestChallengeSolved. -- DonWells
I use two arrays to hold my threads, 10 each.

 public class TwentyThreadsTogether extends Test {
    public BoundedBuffer buffer;
    public PutThread putThreads [];
    public TakeThread takeThreads [];

public void setUp(){ buffer = new BoundedBuffer(); putThreads = new PutThread [10]; takeThreads = new TakeThread [10]; for (int each = 0; each < 10; each++) { putThreads[each] = new PutThread(buffer); takeThreads[each] = new TakeThread(buffer);}}

public void tearDown(){ for (int each = 0; each < 10; each++) { putThreads[each].stop(); takeThreads[each].stop();}}

public void runTest (){ TakeThread.resetOutput(); for (int each = 0; each < 10; each++) { takeThreads[each].start();}; for (int each = 0; each < 10; each++) { putThreads[each].start();}; waitForTakeToFinish(); should(TakeThread.outputLength() == 50, "output was too short"); should(TakeThread.doesOuputHaveTenOf("a"), "should get ten a"); should(TakeThread.doesOuputHaveTenOf("b"), "should get ten b"); should(TakeThread.doesOuputHaveTenOf("c"), "should get ten c"); should(TakeThread.doesOuputHaveTenOf("d"), "should get ten d"); should(TakeThread.doesOuputHaveTenOf("e"), "should get ten e");}

public void waitForTakeToFinish(){ try { for (int each = 0; each < 10; each++) { takeThreads[each].join(4000);}} catch (InterruptedException exception) {};} }
I also modified the TakeThread so that it can hold 50 strings of output and added the doesOuputHaveTenOf method, which is almost identical to the two version. I refactored out the part which counts how many of a string there are into a separate method.

    public static boolean doesOuputHaveTwoOf (String aValue){
        return howManyOf(aValue) == 2;}

public static boolean doesOuputHaveTenOf (String aValue){ return howManyOf(aValue) == 10;}

public static int howManyOf (String aValue){ int howMany = 0; for (int each = 0; each < outputLength; each++){ if (output[each] == aValue) howMany++;} return howMany;}

public static String outputString (){ String outputAsString = ""; for (int each = 0; each < outputLength; each++){ outputAsString += output[each];}; return outputAsString;}

public static int outputLength(){ return outputString().length();}
Now I can try it. This test runs just fine too.

EditText of this page (last edited February 16, 2012) or FindPage with title or text search