action:
public String dWeather() {
WeatherThreadTask weatherThreadTask=new WeatherThreadTask(ip);
weatherThreadPool.execute(weatherThreadTask);
System.err.println(weatherThreadTask.getWeatherInfo());
return "dwea";
}
线程类:
class WeatherThreadTask implements Runnable,Serializable{
private static final long serialVersionUID = 1L;
protected String ip;
private WeatherInfo weatherInfo;
public WeatherInfo getWeatherInfo() {
return weatherInfo;
}
public void setWeatherInfo(WeatherInfo weatherInfo) {
this.weatherInfo = weatherInfo;
}
public WeatherThreadTask(String ip){
this.ip=ip;
}
public void run() {
try{
IWeatherPlugin plugin=PluginManager.getWeatherPlugin();
WeatherInfo weatherinfo=plugin.getToday(ip);
setWeatherInfo(weatherinfo);
}catch(Exception e){
e.printStackTrace();
}
}
}
我想在action获取weatherinfo数据?
怎么做?
1、将WeatherThreadTask 实现Callable接口
class WeatherThreadTask implements Callable,Serializable{
private static final long serialVersionUID = 1L;
protected String ip;
private WeatherInfo weatherInfo;
public WeatherInfo getWeatherInfo() {
return weatherInfo;
}
public void setWeatherInfo(WeatherInfo weatherInfo) {
this.weatherInfo = weatherInfo;
}
public WeatherThreadTask(String ip){
this.ip=ip;
}
public Object call() throws Exception {
WeatherInfo weatherinfo=null;
try{
IWeatherPlugin plugin=PluginManager.getWeatherPlugin();
=plugin.getToday(ip);
setWeatherInfo(weatherinfo);
}catch(Exception e){
e.printStackTrace();
}
return weatherinfo;
}
}
2、main方法调用
public static void main(String[] args) {
WeatherThreadTask wt=new WeatherThreadTask("");
ExecutorService es=Executors.newCachedThreadPool();
Future future=es.submit(wt);
WeatherInfo info=future.get();
}
用future吧
http://blog.csdn.net/yangyan19870319/article/details/6093481