public final class ServiceThread implements Runnable,ISoapResultProcess {
private SOAPServiceManager soapservice;
private ServiceSync obj=new ServiceSync();
private Context mContext;
int value;
private SyncUtils util;
private ISoapResultProcess consumer;
private boolean flag=true;
private ThreadPoolExecutor mExecutor;
private boolean boollock=true;
private Object objnew=new Object();
public ServiceThread(ServiceSync a,Context context) {
this.obj=a;
util = SyncUtils.getInstance(context);
this.mContext=context;
consumer=this;
if (soapservice == null) {
soapservice = new SOAPServiceManager(context);
}
}
public void run() {
// Do sending server update request here
if (soapservice.haveNetworkConnection()) {
int op = obj.getOpconst();
Log.e("val", ""+op);
String strType=obj.getOptype();
Log.e("strType", ""+strType);
boolean boolflag=obj.getOpflag();
if(strType.contentEquals("upload"))
{
util.syncProcess(op, boolflag,consumer);
}else
{
util.downloadsyncProcess(op, boolflag,consumer);
}
flag=util.getOpflag();
synchronized (objnew) {
try {
objnew.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e("runflag0", "-"+flag);
/*if(flag)
{
flag = false;
run();
return;
}*/
return;
}
}
}
@Override
public void processResponse(JSONObject result, String method) {
// TODO Auto-generated method stub
boollock=java.lang.Thread.holdsLock(objnew);
if(boollock)
{
objnew.notify();
}
mExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
ServiceThreadlocal tempThreadServiceThread = new ServiceThreadlocal(1,method,true, mContext,result,consumer);
mExecutor.execute(tempThreadServiceThread);
}
}
以上程序Threadpool执行程序每次接收到不同的对象值时执行。在这个例子中我发送了obj1,obj2。如果obj1执行那个时间,obj2将处于等待阶段。释放下一个对象的对象将被执行。但我的程序第一个对象只执行。注意:执行的第一个对象获取respose然后另一个对象想要执行
答案 0 :(得分:0)
public ServiceThread(ServiceSync a,Context context) {
mExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
this.obj=a;
util = SyncUtils.getInstance(context);
this.mContext=context;
consumer=this;
if (soapservice == null) {
soapservice = new SOAPServiceManager(context);
}
}
//...
@Override
public void processResponse(JSONObject result, String method) {
// TODO Auto-generated method stub
boollock=java.lang.Thread.holdsLock(objnew);
if(boollock)
{
objnew.notify();
}
ServiceThreadlocal tempThreadServiceThread = new ServiceThreadlocal(1,method,true, mContext,result,consumer);
mExecutor.submit(tempThreadServiceThread);
}