Al2O3
Al2O3

Reputation: 3203

Why getStamp() return the same value?

I read this code in Thinking in Java and get puzzled:

    package generics;

    //: generics/Mixins.java
    import java.util.*;

    interface TimeStamped { long getStamp(); }

    class TimeStampedImp implements TimeStamped {
      private final long timeStamp;
      public TimeStampedImp() {
        timeStamp = new Date().getTime();
      }
      public long getStamp() { return timeStamp; }
    }

    interface SerialNumbered { long getSerialNumber(); }

    class SerialNumberedImp implements SerialNumbered {
      private static long counter = 1;
      private final long serialNumber = counter++;
      public long getSerialNumber() { return serialNumber; }
    }

    interface Basic {
      public void set(String val);
      public String get();
    }

    class BasicImp implements Basic {
      private String value;
      public void set(String val) { value = val; }
      public String get() { return value; }
    }

    class Mixin extends BasicImp
    implements TimeStamped, SerialNumbered {
      private TimeStamped timeStamp = new TimeStampedImp();
      private SerialNumbered serialNumber =
        new SerialNumberedImp();
      public long getStamp() { return timeStamp.getStamp(); }
      public long getSerialNumber() {
        return serialNumber.getSerialNumber();
      }
    }

    public class Mixins {
      public static void main(String[] args) {
        Mixin mixin1 = new Mixin(), mixin2 = new Mixin();
        mixin1.set("test string 1");
        mixin2.set("test string 2");
        System.out.println(mixin1.get() + " " +
          mixin1.getStamp() +  " " + mixin1.getSerialNumber());
        System.out.println(mixin2.get() + " " +
          mixin2.getStamp() +  " " + mixin2.getSerialNumber());
        while(true)System.out.println(new Date().getTime());
      }
    } /* Output: (Sample)
    test string 1 1132437151359 1
    test string 2 1132437151359 2
    *///:~

Why are the values returned of getStamp() the same? (1132437151359 == 1132437151359)? Two objects are created and they have different propoties created in different time, so Why?

Upvotes: 1

Views: 146

Answers (3)

Peter Lawrey
Peter Lawrey

Reputation: 533530

The expression new Date().getTime() is a slow way of doing System.currentTimeMillis() which has a minimum resolution of one milli-seconds (but can be as much as 16 ms on some OSes)

This means if the method is called less than one milli-second apart it can give the same result.

A better option is to use AtomicLong.getAndIncrement() for ids.

Upvotes: 1

Maciej Ziarko
Maciej Ziarko

Reputation: 12084

Try something like this:

Mixin mixin1 = new Mixin();
Thread.sleep(10);
Mixin mixin2 = new Mixin();

Now you got 10 ms pause in the process of creating those 2 objects.

Your class is simple and you have fast computer so distance in time between two instantations is so small that Java can't see it.

Upvotes: 0

Vivin Paliath
Vivin Paliath

Reputation: 95518

Using time for serial numbers is not a good idea. The reason you're getting the same time is probably because the code runs rather quickly and enough time doesn't elapse between instantiation of the first object and the second. The time stamp is returned in milliseconds and so if the instantiation of both objects is within 1ms of each other, you won't see a difference.

If you increase load on the system, you might see a difference, or if you use Thread.sleep(5) to cause your program to pause. Both approaches aren't very good.

Instead of using the time for a unique id, use UUID.

Upvotes: 1

Related Questions