OKHttp的单例模式


import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * Created by d on 2015/12/5.
 */

public class OkHttpUtils {

    private static OkHttpClient client = null;

    public static OkHttpClient getInstance(){
        if(client==null){
            synchronized (OkHttpUtils.class){
                if(client==null){
                    client = new OkHttpClient.Builder()
                            .writeTimeout(20000, TimeUnit.SECONDS)
                            .connectTimeout(20000,TimeUnit.SECONDS)
                            .readTimeout(20000,TimeUnit.SECONDS)
                            .build();
                }
            }
        }
        return client;
    }
}

你可能感兴趣的:(OKHttp的单例模式)