改造的`Call :: execute`线程安全吗?

时间:2019-02-27 18:17:46

标签: java kotlin concurrency retrofit2 future

所以我真的不喜欢Call::enqueue的回调方面。

我宁愿将对Call::execute的调用包装到Future中。

做这样的事有多安全

/**An auxiliary retrofit controller to facilitate communication with the server during testing.*/
object Server : CoroutineScope{

    private val job = Job()
    override val coroutineContext: CoroutineContext
        get() = job + Dispatchers.IO

    const val URL = ...

    private val retrofit = Retrofit.Builder()
            .baseUrl(URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(DebugServerAPI::class.java)

    /**Sends the [somethingRequest] to the server.
     * @return a [Deferred] holding the server [Response]*/
    fun trySomething(jwt:String,somethingRequest:JsonObject):Deferred<Response<JsonObject>>{
        return async { retrofit.something(jwt,somethingRequest).execute() }
    }
}

0 个答案:

没有答案