java调Python脚本(五):java通过 Runtime.getRuntime().exec()调Python脚本一直没有返回值,卡住了,数据太大

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ailo555/article/details/82896675

1、背景:

当我们调用系统外部的某个程序,此时就可以用Runtime.getRuntime().exec()来调用。这次java调Python算法代码时,一直没有返回值,卡住了,而用pycharm跑Python代码是正常的,说明是java这边的问题。去看了后台的java进程一直都在,挂住了,找了好久才想到是阻塞问题, 原来是因为Python文件中返回的数据太大而且很多,况且Python运行中出现很多异常警告和提示什么的都是输出控制台,所以不及时捕捉进程的输出,进程就被挂起了。

解决:启动Process 进程后,再启动两个JAVA线程各种类型是数据流及时的把被调用进程的输出截获,完美ok解决。

2、代码实现用线程对数据流的处理类:

/**
 * 
 * 处理流线程
 * 
 * @version 1.0
 * @since JDK1.8
 * @author weijs
 * @date 2018年9月28日 下午1:31:59
 */
public class StreamThread extends Thread {

	private Logger logger = Logger.getLogger(getClass());
	// 输入流
	private InputStream inputStream;
	// 流类型
	private String streamType;
	// 是否运行完成
	private volatile boolean inFinish = false;
	// 需要的返回结果数据
	private Map<String, JSONArray> resultMap;

	/**
	 * 构造器
	 * 
	 * @param inputStream 输入流
	 * @param streamType 流类型
	 */
	public StreamThread(InputStream inputStream, String streamType) {
		this.inputStream = inputStream;
		this.streamType = streamType;
		this.inFinish = false;
		this.resultMap = new HashMap<>();
	}

	/**
	 * 重写run()方法
	 */
	public void run() {
		try {
			InputStreamReader isr = new InputStreamReader(inputStream, "GBK");
			BufferedReader bufferedReader = new BufferedReader(isr);
			String line;
			while ((line = bufferedReader.readLine()) != null) {
				if (streamType.equals("Error")) {
					logger.error(">>>>>>Error :" + line);
				} else {
					logger.info("info:" + line);
					if (line.contains("[{")) {// Python返回的json数据
						logger.debug("---------最想要的结果:" + line);
						if (line.contains("back_result:")) {
							String back = line.substring(line.indexOf("[{"), line.length());
							JSONArray fromObject = JSONObject.parseArray(back);
							resultMap.put("back", fromObject);
						}
						if (line.contains("filter_result:")) {
							String string = line.substring(line.indexOf("[{"), line.length());
							JSONArray fromObject = JSONObject.parseArray(string);
							resultMap.put("filter", fromObject);
						}
						if (line.contains("buy_result:")) {
							String string = line.substring(line.indexOf("[{"), line.length());
							JSONArray fromObject = JSONObject.parseArray(string);
							resultMap.put("buy", fromObject);
						}
					}
				}
			}
			isr.close();
			bufferedReader.close();
		} catch (IOException e) {
			logger.error("Exception:" + e);
			e.printStackTrace();
		} finally {
			this.inFinish = true;
			synchronized (this) {
				notify();
			}
		}
	}

	/**
	 * 
	 * 返回结果
	 *
	 * @return
	 * 
	 * @author weijs
	 * @date 2018年9月28日 下午1:37:42
	 */
	public Map<String, JSONArray> getContent() {
		if (!this.inFinish) {
			synchronized (this) {
				try {
					wait();
				} catch (InterruptedException ignore) {
					ignore.printStackTrace();
				}
			}
		}
		return this.resultMap;
	}

}

3、开启2个线程分别跑错误流和数据返回流的处理:

猜你喜欢

转载自blog.csdn.net/ailo555/article/details/82896675