易截截图软件、单文件、免安装、纯绿色、仅160KB

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


相关文档:

Java解惑2 23不劳无获

下面的程序将打印一个单词,其第一个字母是由一个随机数生成器来选择的。请描述该程序的行为:
import java.util.Random;
public class Rhymes {
private static Random rnd = new Random();
public static void main(String[] args) {
StringBuffer word = null;
switch(rnd.nextInt(2)) {
......

Java解惑3 24尽情享受每一个字节

下面的程序循环遍历byte数值,以查找某个特定值。这个程序会打印出什么呢?
public class BigDelight {
public static void main(String[] args) {
for (byte b = Byte.MIN_VALUE; b < Byte.MAX_VALUE; b++) {
if (b == 0x90)
System.out.print("Joy!");
}
......

Java解惑3 30循环者的爱子

请提供一个对i的声明,将下面的循环转变为一个无限循环:
while (i != i + 0) {
}
与前一个谜题不同,你必须在你的答案中不使用浮点数。换句话说,你不能把i声明为double或float类型的。
与前一个谜题一样,这个谜题初看起来是不可能实现的。毕竟,一个数字总是等于它自身加上0,你被禁止使用浮点数,因此不能使用NaN ......

Java解惑3 31循环者的鬼魂

请提供一个对i的声明,将下面的循环转变为一个无限循环:
while (i != 0) {
i >>>= 1;
}
回想一下,>>>=是对应于无符号右移操作符的赋值操作符。0被从左移入到由移位操作而空出来的位上,即使被移位的负数也是如此。
这个循环比前面三个循环要稍微复杂一点,因为其循环体非空。在其循环题中, ......

Java解惑3 35一分钟又一分钟

下面的程序在模仿一个简单的时钟。它的循环变量表示一个毫秒计数器,其计数值从0开始直至一小时中包含的毫秒数。循环体以定期的时间间隔对一个分钟计数器执行增量操作。最后,该程序将打印分钟计数器。那么它会打印出什么呢?
public class Clock {
public static void main(String[] args) {
int minutes ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号