日期:2014-05-20 浏览次数:20738 次
public class Mytest {
public void print() {
System.out.println("123");
}
public void print2() {
System.out.println("1234");
}
public static void main(String[] arg) throws InterruptedException {
Mytest test = new Mytest();
test.print();
synchronized(test){
test.wait(2000);
}
test.print2();
}
}
//实现自己的timer
class MyTimer
{
//用于累加周期,我就不考虑数据溢出啥的了,估计也溢出不了
long start = 0L;
long end = 0L;
//控制时钟周期
final long invet = 2000L;
//这个运算可能要消耗1ms 忽略不计
boolean action()
{
start = System.currentTimeMillis();
if (start > end)
{
end = start + invet;
return true;
}
else
{
return false;
}
}
}
public class TimerTest
{
public static void main(String[] args) throws Exception
{
MyTimer timer = new MyTimer();
while (true)
{
System.out.println("t1");
if (timer.action())
{
System.out.println("t2-------------------我来了");
//为了方便看效果,停一下
Thread.sleep(100);
}
}
}
}