做眼镜网站草图有什么原则/更先进的seo服务
Timer和TimerTask是util包中两个与工作排程的类,Timer是计时器,可以设定成特定时间,不过这里只有Timer不能单独使用,必须和TimerTask一起才有作用。Timer一旦与某个TimerTask产生关联,就会在产生信号的同时,连带一起执行TimerTask所定义的工作。
TimerTask的实现只需要继承TimerTask类就并实现其run()方法就可以了。run()方法是由我们自己来编写的,把你想做的工作放在里面,一旦Timer在特定时间内或周期产生信号,run()方法就会执行,我们通会Timer的schdeule()方法来设定特定时间或特定的周期。schdeule()有两种形式,一个是两个参数的,一个是三个参数的。二种参数的第一个参数是TimerTask的对象,第二个是时间也可是以Date对象。具有三个参数的schedule方法可以使一个task在某一个时间后,根据一定的间隔时间运行多次,具有周期性。最后,可以使用Timer的cancel()方法来停止Timer,调用cancel()之后,两者就会脱离关系。TimerTask本身也有cancel()方法。
今天无意间看到了ByteArrayOutputStream、ByteArrayInputStream,他们可以捕获到缓存中的数据,转换为字节数组,一下是下载图片的时候使用到的:
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
try {
URL url = new URL(path);
HttpURLConnection coon = (HttpURLConnection) url.openConnection();
coon.setConnectTimeout(5000);// 设置连接超时
coon.setDoInput(true);// 拿到连接对象
coon.setRequestMethod("GET");
// 获取响应状态码
int code = coon.getResponseCode();
if (code == 200) {// 获取成功
InputStream is = coon.getInputStream();
// 拿到输入流,用于读取响应的内容
byte data[] = new byte[1024];
int len;
while ((len = is.read(data)) != -1) {
bStream.write(data, 0, len);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return bStream.toByteArray();
转载于:https://blog.51cto.com/shunshuncon/1413365