Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6187155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:00:41+00:00 2026-05-24T02:00:41+00:00

I want to have thread save method that returns a unique current Timestamp.Even when

  • 0

I want to have thread save method that returns a unique current Timestamp.Even when the method is called by same time i want to get a unique current datetime.Even if this method is called by making multiple instances of MyClass i want it be be thread safe always

class Myclass{

 Date getUniquetimeStam(){
   synchronized(Myclass.class){
    //return date here 
   }

}

Now if i make 2 instances of Myclass and call getUniqueStam at same ,is it gaurented to return uniue date time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:00:41+00:00Added an answer on May 24, 2026 at 2:00 am

    No, you are not guaranteed. If your computer is fast enough then both method calls can happen in same millisecond and produce identical Date object.

    You have 2 options here:

    1. Use UUID instead of date. Read more about it here . UUID by specification is guaranteed to be unique each time you generate it – so it’s the safest and easiest option you can get.
    2. Store the date object, synchronize on it, and check if it’s the same. Here’s an example:

    .

    class Myclass {
    
        //Static to make it possible to synchronize between instances of Myclass
        static Date lastDate;
    
        Date getUniqueTimeStamp() {
            synchronized (Myclass.lastDate) {
                Date newDate = new Date();
                if (newDate.getTime() <= Myclass.lastDate.getTime()) {
                    newDate.setTime(Myclass.lastDate.getTime()+1);
                }
                Myclass.lastDate.setTime(newDate.getTime());
                return newDate;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Ruby, I want to have two threads running at the same time, and
Maybe i cant do what i want? I want to have 1 thread do
I have several thread pools and I want my application to handle a cancel
I want to have a select-only ComboBox that provides a list of items for
I want to have a text box that the user can type in that
I have an object with a CookieJar that I want to pickle. However as
Here's the background, I have a webservice from a vendor/partner that returns a large
I have called a native program which creates another thread, which attaches itself to
I have multiple threads that are processing rows from the same table that is
I have an array of threads, and I want to Join them all with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.