Skip to content

Conversation

@m1a2st
Copy link
Collaborator

@m1a2st m1a2st commented Jun 3, 2024

https://issues.apache.org/jira/browse/KAFKA-16879

I use the static inner class to implement to singleton pattern.

Committer Checklist (excluded from commit message)

  • Verify design and implementation
  • Verify test coverage and CI build status
  • Verify documentation (including upgrade notes)

public interface Time {

Time SYSTEM = new SystemTime();
Time SYSTEM = SystemTime.getInstance();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, is there a reason this isn't the canonical singleton instance? Why would we want two different ways to get the singleton Time.SYSTEM and SystemTime.getInstance()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I should deprecate Time SYSTEM this property, and use SystemTime.getInstance() to instead it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be inclined to keep the existing Time.SYSTEM and avoid the deprecation. SystemTime can then be package-local entirely.

Comment on lines +30 to +31
* use the static inner class to implement the singleton pattern
* to avoid the overhead of synchronization
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this comment? I don't know what it's referring to, as none of this code is synchronized.

The static inner class seems to be a lazy initialization trick, which is ineffective here (due to Time.SYSTEM) and unneeded (since the SystemTime constructor is very cheap).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, SystemTime is very cheap, but I think that don't use double-checked locking to implement the singleton pattern, maybe I should remove this comment is more better.

@m1a2st m1a2st closed this Jun 4, 2024
@m1a2st m1a2st deleted the KAFKA-16879 branch June 4, 2024 01:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants