Govind Singh
Govind Singh

Reputation: 15490

Efficent use of Future

is there any diffrence between in these approach?

val upload = for {
              done <- Future {
              println("uploadingStart")
              uploadInAmazonS3             //take 10 to 12 sec
              println("uploaded")
              }
}yield done

println("uploadingStart")
val upload = for {
              done <- Future {
               uploadInAmazonS3            //take 10 to 12 sec
              }
}yield done
println("uploadingStart")

i wanna know in terms of thread Blocking?

does thread is blocked here, while executing these three lines

println("uploadingStart")
uploadInAmazonS3             //take 10 to 12 sec
println("uploaded")

and in another it is not blocking thread it is so? or thread are same busy in both cases?

Upvotes: 0

Views: 70

Answers (2)

Vishal John
Vishal John

Reputation: 4382

The code within future will be executed by some thread from the executionContext(thread pool)

Yes, the thread which executes this part

println("uploadingStart")
uploadInAmazonS3             //take 10 to 12 sec
println("uploaded")

will be blocked, but not the calling thread(main thread).

In the second case both the println statements are executed by the main thread. Since the main thread simply proceeds after creating the future, the println statements are executed without any delay

Upvotes: 1

cchantep
cchantep

Reputation: 9158

The difference is that in former code, println are executed when the future is really performed, whereas in the second one println are runed when the future is declared (prepared, but not yet executed).

Upvotes: 1

Related Questions