Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21206 rajender 1
package com.saholic.profittill.Services;
2
 
3
import android.util.Base64;
4
 
5
import org.apache.http.HttpResponse;
6
import org.apache.http.NameValuePair;
7
import org.apache.http.client.ClientProtocolException;
8
import org.apache.http.client.HttpClient;
9
import org.apache.http.client.entity.UrlEncodedFormEntity;
10
import org.apache.http.client.methods.HttpPost;
11
import org.apache.http.impl.client.DefaultHttpClient;
12
import org.apache.http.message.BasicNameValuePair;
13
import org.json.JSONObject;
14
 
15
import java.io.IOException;
16
import java.util.ArrayDeque;
17
import java.util.ArrayList;
18
import java.util.Queue;
19
import java.util.concurrent.Executor;
20
 
21
public class SerialExecutor implements Executor {
22
    final Queue<Runnable> tasks = new ArrayDeque();
23
    static Executor singletonExector;
24
    Executor executor = new Executor() {
25
        @Override
26
        public void execute(Runnable command) {
27
            new Thread(command).start();
28
        }
29
    };
30
    Runnable active;
31
 
32
    public synchronized void execute(final Runnable r) {
33
        tasks.offer(new Runnable() {
34
            public void run() {
35
                try {
36
                    r.run();
37
                } finally {
38
                    scheduleNext();
39
                }
40
            }
41
        });
42
        if (active == null) {
43
            scheduleNext();
44
        }
45
    }
46
 
47
    protected synchronized void scheduleNext() {
48
        if ((active = tasks.poll()) != null) {
49
            executor.execute(active);
50
        }
51
    }
52
    public static Executor getInstance(){
53
        if (singletonExector==null) {
54
            singletonExector = new SerialExecutor();
55
 
56
        }
57
        return singletonExector;
58
    }
59
}
60