How to download files in parallel using Channels and Scala?

How to make this code download files in parallel?

object Files{
  def main(args: Array[String]): Unit ={

    System.setProperty("http.proxyHost", "abc");
    System.setProperty("http.proxyPort", "8080");

    var num = "000"
    for(x <- 1 to 999){
      if(x < 10){
        num = ("00" + x).toString
      }else if(x < 100 && x > 9){
        num = ("0" + x).toString
      }else{
        num = x.toString
      }
      println(num)
      var  myUrl = new URL("http://www.example.com/1" + num + ".jpg");
      var myUrlStream = myUrl.openStream();
      var myUrlChannel = Channels.newChannel(myUrlStream);

      var destinationChannel = new FileOutputStream("C:\\10"\\ + num + ".jpg").getChannel();
      try{
        destinationChannel.transferFrom(myUrlChannel, 0, Integer.MAX_VALUE);
      }catch{
        case ioe: IOException => ioe.printStackTrace()
      }
    }
  }
}

Answers


You can use parallel collections (notice the par call):

(1 to 999).par.foreach { x =
  if(x < 10){
    num = ("00" + x).toString
  }else if(x < 100 && x > 9){
    num = ("0" + x).toString
  }else{
    num = x.toString
  }
  println(num)
  var  myUrl = new URL("http://www.example.com/1" + num + ".jpg");
  var myUrlStream = myUrl.openStream();
  var myUrlChannel = Channels.newChannel(myUrlStream);

  var destinationChannel = new FileOutputStream("C:\\10"\\ + num + ".jpg").getChannel();
  try{
    destinationChannel.transferFrom(myUrlChannel, 0, Integer.MAX_VALUE);
  }catch{
    case ioe: IOException => ioe.printStackTrace()
  }
}

Need Your Help

Nullreference when trying to set ACL property of new Parse object

parse-platform unity3d nullreferenceexception acl

As the title already explains I have null reference exception when I try to set the ACL property of my new parse object.

javascript force next input if condition meet

javascript jquery validation

I'm currently using jQuery liveform validation plugin to validate the input column if condition is meet and I use below code to force next if the input button reach the max input.