android 线程判断超时 通俗易懂!

研究了 线程超时方面的东西
基本思路
触发事件之后 同时开启2个线程
   1-timer线程
   2-执行数据访问的thread线程
   3-Runnable线程 此线程用来更新UI
   timer线程设置CHECK_TIME秒之后执行,也就是访问的最大时间 超过此时间就视为超时
那么我们应该考虑的就是超时 和 不超时的处理
    假如超时--
        也就是说 timer已经执行了,那我们就应该把 thread线程停止掉 安全起见我用个boolean值进行限制。然后再thread线程内判断此boolean值 是否继续下面的操作。
    假如正常--
        当thread线程访问正常速度在5秒之内,也就是说thread赶在 timer之前执行完毕了,那就按照我们正常的思路来写程序,当然别忘了把timer停止掉
  

	public void onScanReady(final String jan) {
		isTimeOut = false;
//		Log.d("#", "onScanReady  > ");
		showWaitDialogNoTitle(getString(R.string.MSG_I_0004));
		final Runnable mUpdateResults = new Runnable() {
			public void run() {
				if (isTimeOut) {
//超时操作
				} else {
					initListView(jan);
				}
			}
		};
		// //////////
		if (thread != null) {
			thread.stop();
		}
		timer = new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
//				Log.d("#", "timer out 1> ");
				// 若5秒之后执行了这个 说明已经超时 在访问数据库的线程里面关闭此timer
				isTimeOut = true;
				if (thread != null) {//停掉数据访问线程
					thread.stop();
				}
				uiHandler.post(mUpdateResults);//更新UI
//				Log.d("#", "timer out 2> ");
			}
		}, CHECK_TIME);
//		Log.d("#", "start thread > ");
		thread = new Thread(new Runnable() {
			@Override
			public void run() {
				Looper.prepare();
				try {
					list = countListBll.getAllByJan(handler, jan);//数据访问操作
					if (!isTimeOut) {
						uiHandler.post(mUpdateResults); // call updateUI thread
						timer.cancel();
//						Log.d("#", "         success > ");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
                        Looper.loop();
			}
		});
		thread.start();
	}


下面是 用了两个thread 一个是守护线程 另一个是查询线程
用Thread.run() 两个方法不会同时运行 需要用start()方法
@Override
	public void onScanReady(String jan) {
		if (jan.replaceFirst("^0*", "").length() >= 13) {
			jan = jan.replaceFirst("^0*", "").substring(0, 13);
		}
		//判断jan
		if(!countListBll.checkProductCD(jan)){
			showAlertDialog_OK(getString(R.string.MESSAGE_CODE_U0015),
					getString(R.string.MSG_COMMON_YES),null);
			return; 
		}
		final String productCD=jan;
		Log.i(LOG_TAG,"CountConfirmList > onScanReady() > ProductCD:"+productCD);
		Boolean isNetwork = Utility
				.isNetworkAvailable(InventoryCountConfirm_ListActivity.this);
		if (isNetwork) {
			isTimeOut = false;
			showWaitDialogNoTitle(getString(R.string.MSG_I_0004));
			final Runnable mUpdateResults = new Runnable() {
				public void run() {
					if (isTimeOut) {
						String msg = getString(R.string.MESSAGE_CODE_U0014);
						String okStr = getString(R.string.MSG_COMMON_OK);
						showAlertDialog_OK(msg, okStr, null);
					} else {
						initListView();
					}
				}
			};
			if (thread != null) {
				thread.stop();
			}
			checkThread=new Thread(new Runnable() {
				
				@Override
				public void run() {
					try {
						Log.e("====", " checkThread >>>");
						Thread.sleep(5000);
						Log.e("====", " isTimeOut >>>");
						if (thread != null) {
							thread.stop();
							isTimeOut = true;
						}
						Log.i(LOG_TAG,"CountConfirmList > onScanReady() > TimeOut");
						uiHandler.post(mUpdateResults);//Ui update
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			});
			checkThread.setDaemon(true);
			checkThread.start();
			thread = new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						Log.e("====", " thread >>>");
						list = countListBll.getAllByJan(handler, productCD);//
//						Thread.sleep(6000);
						if (!isTimeOut) {
							uiHandler.post(mUpdateResults); // call updateUI thread
//							timer.cancel();
							if (checkThread!=null) {
								checkThread.interrupt();
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
			thread.start();
		}else{
			 String msg = getString(R.string.MSG_E_0003);
			 String okStr = getString(R.string.MSG_COMMON_OK);
			 showAlertDialog_OK(msg, okStr, null);
		}
	}

猜你喜欢

转载自44289533.iteye.com/blog/1714483