title: Scala Thread Pool - Invoking API's Concurrently
body: <p><br>
  I have a use-case in databricks where an API call has to me made on a dataset of URL's. The dataset has around 100K records.
  The max allowed concurrency is 3.<br>
  I did the implementation in Scala and ran in databricks notebook. Apart from the one element pending in queue, i feel some thing is missing here.<br>
  Is the Blocking Queue and Thread Pool the right way to tackle this problem.</p>
  
  <p>In the code below I have modified and instead of reading from dataset I am sampling on a Seq. 
  Any help/thought will be much appreciated. </p>
  
  <pre> <code>
  import java.time.LocalDateTime
  import java.util.concurrent.{ArrayBlockingQueue,BlockingQueue}
  import java.util.concurrent.Executors
  import java.util.concurrent.TimeUnit; 
  
  var inpQueue:BlockingQueue[(Int, String)] = new ArrayBlockingQueue[(Int, String)](1)
  
  val inpDS = Seq((1,"https://google.com/2X6barD"), (2,"https://google.com/3d9vCgW"), (3,"https://google.com/2M02Xz0"), (4,"https://google.com/2XOu2uL"), (5,"https://google.com/2AfBWF0"), (6,"https://google.com/36AEKsw"), (7,"https://google.com/3enBxz7"), (8,"https://google.com/36ABq0x"), (9,"https://google.com/2XBjmiF"), (10,"https://google.com/36Emlen"))
  
  
  val pool = Executors.newFixedThreadPool(3) 
  var i = 0
  inpDS.foreach{
    ix => {
  
      inpQueue.put(ix)
      val t = new ConsumerAPIThread()
      t.setName("MyThread-"+i+" ")
      pool.execute(t)
  
    }
     i = i+1
  }
  
  println("Final Queue Size = " +inpQueue.size+"\n")
  
  
  class ConsumerAPIThread() extends Thread  
  { 
    var name =""
  
      override def run() 
      { 
          val urlDetail =  inpQueue.take()
          print(this.getName()+" "+ Thread.currentThread().getName() + " popped "+urlDetail+" Queue Size "+inpQueue.size+" \n") 
        triggerAPI((urlDetail._1, urlDetail._2))
      } 
  
      def triggerAPI(params:(Int,String)){
  
      try{
        val result = scala.io.Source.fromURL(params._2)
        println("" +result)
      }catch{
       case ex:Exception  => {
  
         println("Exception caught")
         }
  
      }
  
    }
     def ConsumerAPIThread(s:String) 
      { 
          name = s; 
      } 
  }
  </code></pre>