不会是什么时候new的就是什么值了吧?我考,java里每次想得到系统时间都要new一个Date么?

解决方案 »

  1.   

    new的时候什么值以后就是什么值。除非手动改掉。
      

  2.   

    原来如此,再问一下,回答了马上就结。
    多线程环境下,System.currentTimeMillis()能否保证每次返回不同的值呢?
    我觉得不能,毕竟毫秒对于cpu来说太长了。
    那么每次产生一个类似于GUID的函数是什么?多线程环境下的。
      

  3.   

    我现在用的是这种方法:
    synchronized (this) {
        GUID = System.currentTimeMillis();
        wait(1);
    }
    好像效率不好。
      

  4.   

    多线程环境下,System.currentTimeMillis()能保证每次返回不同的值
      

  5.   

    楼上的,想想再说,多个CPU的话同时调可能返回不同的时间吗?
    好歹你也试一下再说啊。不要说多线程了,我单线程连着写:
    long time1 = System.currentTimeMillis();
    long time2 = System.currentTimeMillis();
    long time3 = System.currentTimeMillis();
    ...
    在我的p4 2G上全都一个数。
    最ft的是中间加上wait(1)都不好使。
      

  6.   

    你将wait(1)换成wait(2000)试试看
      

  7.   

    看看currentTimeMillis的api吧,currentTimeMillis返回的是毫秒级的时间单位,the granularity of the value depends on the underlying operating system and may be larger它们的间隔时间依据操作系统可能会很大(换言子也可能很小,当然和硬件速度也会有关系了)wait(1)是等待1毫秒的意思,所以建议你换成wait(1000)试试看
    public static long currentTimeMillis()
    -------------------
    Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.
      

  8.   

    刚才上机测试了一下,问题并非上述
    你原先的例子应该是
                Date date=new Date();
                long time1 = date.getTime();
                synchronized(this) {
                    this.wait(1);
                }
                long time2 = date.getTime();
    time1和time2都是来自同一个对象,所以,不管你中间wait多少秒,它们的值肯定都是一样的应该换成:            long time1 = new Date().getTime();
                synchronized(this) {
                    this.wait(1);
                }
                long time2 = new Date().getTime();
      

  9.   

    楼上说得我已经知道了。前面几帖回复了。wait(1000)更不可能,作为服务器,wait(1)我已经觉得是一个很大的损失了。我根本不想wait,如果哪位大侠知道该如何不用wait也能像C里面调UuidCreate那样,可以随时产生一个不同的串,感激不尽。
    我相信有很方便的方法,只是对java不熟啊。
      

  10.   

    原来是想生成唯一的字符串啊?
    不知道这个符不符合你的要求:
    http://expert.csdn.net/Expert/topic/3066/3066421.xml?temp=.2290308