title: Swap using two threads
body: <p>In process of learning MUltithreading I taught to solve below mentioned problem</p>
  
  <p>You have a String array of alphabets in asc order, the last spot being empty.</p>
  
  <p>Have 2 threads handle the swapping of alphabets, one thread taking letter from the front (i.e., 'a') and swapping with the empty spot, while the second thread takes letter from last (i.e., 'z') and moving it to the empty spot on front</p>
  
  <blockquote>
    <p><strong>Input</strong>:- ABCDEFGHIJKLMNOPQRSTUVWXYZ_<br>
    <strong>Ouput</strong>:-ZYXWVUTSRQPON_MLKJIHGFEDCBA</p>
  </blockquote>
  
  <p>Below is the program I have dome for this problem. But I need to know is there better way to achieve same. </p>
  
  <p>//AlphaSwap</p>
  
  <pre><code>public class AlphaSwap {
  
      String input[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
              "t", "u", "v", "w", "x", "y", "z", "_" };
      int start = 0;
      int end = 26;
      boolean flag;
  
      private String[] swap(String input[], int start, int end) {
          String temp = input[start];
          input[start] = input[end];
          input[end] = temp;
  
          return input;
      }
  
      public synchronized void swapLast(String input[]) {
          if( flag){
              try {
                  wait();
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
          String[] swap = swap(input, start, end);
          System.out.println(Thread.currentThread().getName());
          for (String string : swap) {
              System.out.print(string);
          }
          System.out.println();
          this.end--;
          flag = true;
          notify();
      }
  
      public synchronized void swapFirst(String input[]) {
          if(!flag){
              try {
                  wait();
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
          String[] swap = swap(input, start, end);
          System.out.println(Thread.currentThread().getName());
          for (String string : swap) {
              System.out.print(string);
          }
          System.out.println();
          this.start++;
          flag = false;
          notify();
      }
  
  
  }
  </code></pre>
  
  <p>//Runner main class</p>
  
  <pre><code>public class RunnerMain {
      public static void main(String[] args) {
          AlphaSwap alphaSwap = new AlphaSwap();
  
          new SwapLast(alphaSwap);
          new SwapFirst(alphaSwap);
      }
  }
  </code></pre>
  
  <p>//SwapFirst</p>
  
  <pre><code>public class SwapFirst implements Runnable{
  
      private AlphaSwap swap;
  
      public SwapFirst(AlphaSwap swap) {
          this.swap = swap;
          new Thread(this).start();
      }
  
      @Override
      public void run() {
              for (int i = 0; i &lt; 13; i++) {
                  swap.swapFirst(swap.input);
          }
      }
  }
  </code></pre>
  
  <p>//swapLast</p>
  
  <pre><code>public class SwapLast implements Runnable {
      private AlphaSwap swap;
  
      public SwapLast(AlphaSwap swap) {
          this.swap = swap;
          new Thread(this).start();
      }
  
      @Override
      public void run() {
              for (int i = 0; i &lt; 13; i++) {
                  swap.swapLast(swap.input);
          }
      }
  
  }
  </code></pre>
  
  <p>Any improvement in above code can be done? </p>