Java Timer 对象创建后使用Timer更改其属性!!!
首先来个简单那的实例:
package cn.vicky;
import java.util.Timer;
import java.util.TimerTask;
public class MyTimer {
private int i = 1;
private void change(long time){
System.out.println("one : " + i);
final Timer timer = new Timer();
timer.schedule(new TimerTask(){
@Override
public void run() {
System.out.println("run " + i++);
if (i != 5) {
timer.cancel();
}
}
}, 10000,1);
System.out.println("two : " + i);
}
public static void main(String[] args) {
MyTimer mytimer = new MyTimer();
mytimer.change(0);
}
}
我们以一个实例思考,车站,每辆车只能停靠10分钟就必须出站.传统方式,我们是以车站为单位,通过多线程来控制每辆车的出站情况!
package cn.vicky;
import java.util.Date;
/**
*
* @author Vicky
* 公交汽车
*/
public class Bus {
private String name = "";
private boolean inStation = true;
/**
* 公交汽车在车站中只可能呆10分钟,然后公交就开走,相对于公交车站,公交10分钟后就为空!
*/
public Bus(String carName) {
this.name = carName;
System.out.println(new Date() + " : a car["+ name +"] into the bus station !");
}
public void setInStation(boolean inStation) {
this.inStation = inStation;
}
public boolean isInStation() {
return inStation;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Bus other = (Bus) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "Bus [" + name + "] :: " + (inSta
相关文档:
List的用法
List包括List接口以及List接口的所有实现类。因为List接口实现了Collection接口,所以List接口拥有Collection接口提供的所有常用方法,又因为List是列表类型,所以List接口还提供了一些适合于自身的常用方法,如表1所示。
表1 List接口定义的常用方法及功能
从表1可以看出,List接口提供的适合于自身的 ......
JAVA的容器---List,Map,Set
Collection
├List
│├LinkedList
│├ArrayList
│└Vector
│ └Stack
└Set
Map
├Hashtable
├HashMap
└WeakHashMap
Collection接口
Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements)。一些 Collection允许相 ......
The goal for this chapter is to introduce you to Java's native methods. If you are new to Java, you may not know what native methods are, and even if you are an experienced Java developer, you may not have had a reason to learn more about native methods. At the conclusion of this chapter you should ......
请提供一个对i的声明,将下面的循环转变为一个无限循环:
while (i != 0) {
i >>>= 1;
}
回想一下,>>>=是对应于无符号右移操作符的赋值操作符。0被从左移入到由移位操作而空出来的位上,即使被移位的负数也是如此。
这个循环比前面三个循环要稍微复杂一点,因为其循环体非空。在其循环题中, ......
与谜题26和27中的程序一样,下面的程序有一个单重的循环,它记录迭代的次数,并在循环终止时打印这个数。那么,这个程序会打印出什么呢?
public class Count {
public static void main(String[] args) {
final int START = 2000000000;
int count = 0;
for (float f = START; f < S ......