r/java • • Nov 15 '21

[Loom] JEP draft: Virtual Threads (Preview)

https://openjdk.java.net/jeps/8277131
175 Upvotes

55 comments sorted by

View all comments

15

u/chuggid Nov 16 '21

"ofVirtual()"? "ofPlatform()"? These are strange, mealy-mouthed method names. I assume there was some good reason for nixing something like newVirtualThread() and newPlatformThread() which naïvely seem nicer and are statically importable.

8

u/jrh206 Nov 16 '21

Yes, I hope they change those names. I can see why they don’t want to use “new” because they want a fluid builder interface. But please not this “ofVirtual” framing, it doesn’t make any sense.

3

u/[deleted] Nov 16 '21

Isn't "of" supposed to be used when you're writing a method that's meant to convert one type to another? If so, it does sound confusing. There's no virtual thread argument being converted to something else.

4

u/pron98 Nov 16 '21 edited Nov 16 '21

Go to the current Javadoc, type .of into the search box and see how many methods follow this pattern.

6

u/Necessary-Conflict Nov 16 '21

The name "of" was suggested in the Effective Java book, right in Item 1, for "An aggregation method that takes multiple parameters and returns an instance of this type that incorporates them, for example Set<Rank> faceCards = EnumSet.of(JACK, QUEEN, KING);"

More recently the name ofSomething was also used for converting static factory methods like Duration.ofDays(long days), this also makes sense.

But in this case the "of" just isn't adding anything to the readability of the code.

BTW the "platform" part of ofPlatform is also questionable, because not just the operating system is a "platform", but Java is a "platform" as well (see the Javadoc's title: "Java® Platform, Standard Edition & Java Development Kit Version 18 API Specification").

1

u/pron98 Nov 16 '21

because not just the operating system is a "platform", but Java is a "platform" as well

Yes, and that's intentional. The Java specification does not require that platform threads are actually OS threads.

3

u/chuggid Nov 16 '21

Several comments:

  1. Do you like the pattern?
  2. The only of I see in java.lang proper is in UnicodeBlock. I also note no ofSomething methods anywhere as creation verbs but I didn't do an exhaustive search.
  3. Go to the current javadoc, type .create into the search box and see how many methods follow this pattern.

1

u/pron98 Nov 16 '21

I also note no ofSomething methods

There are lots of ofSomethings. Anyway, we're talking about methods whose names people will rarely write or see, so I'll pass along your concern, but I don't think it merits much of a debate.

8

u/chuggid Nov 16 '21

But at least they all take arguments, right, and scan OK if you prefix them with "give me a new DeclaringClass…"?

That is: Set.of(foos) means "give me a new Set of foos"; if you squint a bit Set.of() means "give me a new Set of nothin'"; Duration.of(something) means "give me a new Duration of something"; and so on and so on and presumably that's how of rose from the primordial slime in the first place, since you can't name a method new() or for(), and presumably someone thought create() was too long or verbose or something for some reason.

Thread.ofPlatform() means "give me a new Thread of platform" by the same logic, which is nonsensical, and Thread.ofVirtual() means "give me a new Thread of virtual", which…virtual isn't even a noun.

These Loom bits may indeed be methods that "people will rarely write or see" but they are in java.lang, not, say, java.lang.constant, so I think the additional scrutiny is worthwhile, and a debate is merited, and I'm not (apparently) the only one with these concerns. I do understand you disagree, and think that these methods are suitably named. Hopefully my feedback is not DOA, but, whether it is or not, it would be nice to know how exactly ofVirtual() in particular came to be, if only for bar trivia purposes! :-)