Android Open Source - foxail_reader Connection Manager






From Project

Back to project page foxail_reader.

License

The source code is released under:

GNU General Public License

If you think the Android project foxail_reader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.foxail.android.common.http;
/*  w w  w. ja va2s.  co m*/
import java.util.ArrayList;  

/** 
 * Simple connection manager to throttle connections 
 *  
 * @author Greg Zavitz 
 */  
public class ConnectionManager {
    
    public static final int MAX_CONNECTIONS = 5;
 
    private ArrayList<Runnable> active = new ArrayList<Runnable>();
    private ArrayList<Runnable> queue = new ArrayList<Runnable>();
 
    private static ConnectionManager instance;
 
    public static ConnectionManager getInstance() {
        if (instance == null)
            instance = new ConnectionManager();
        return instance;
    }
 
    public void push(Runnable runnable) {
        queue.add(runnable);
        if (active.size() < MAX_CONNECTIONS)
            startNext();
    }
 
    private void startNext() {
        if (!queue.isEmpty()) {
            Runnable next = queue.get(0);
            queue.remove(0);
            active.add(next);
 
            Thread thread = new Thread(next);
            thread.start();
        }
    }
 
    public void didComplete(Runnable runnable) {
        active.remove(runnable);
        startNext();
    }
 
}




Java Source Code List

org.foxail.android.common.CommonUtil.java
org.foxail.android.common.http.ConnectionManager.java
org.foxail.android.common.http.HttpConnection.java
org.foxail.android.common.volley.HtmlRequest.java
org.foxail.android.reader.activity.BaseActivity.java
org.foxail.android.reader.activity.FavoritesActivity.java
org.foxail.android.reader.activity.MainActivity.java
org.foxail.android.reader.activity.NewsActivity.java
org.foxail.android.reader.client.ClientFactory.java
org.foxail.android.reader.client.Client.java
org.foxail.android.reader.client.CnBetaClient.java
org.foxail.android.reader.model.News.java