Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Comparison of C Sharp and Java
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Task-based parallelism for Java === Java supports threads since JDK 1.0. Java offers a high versatility for running threads, often called tasks. This is done by implementing a functional interface (a {{Javadoc:SE|package=java.lang|java/lang|Runnable}} interface) defining a single void no-args method as demonstrated in the following example: <syntaxhighlight lang="Java" line> var myThread = new Thread(() -> { var threadName = Thread.currentThread().getName(); System.out.println("Hello " + threadName); }); myThread.start(); </syntaxhighlight> Similar to C#, Java has a higher level mechanism for working with threads. {{mono|Executors}} can execute asynchronous tasks and also manage a group of subprocesses. All the threads of an {{mono|ExecutorServices}} instance are handled in a [[Pool (computer science)|pool]]. This {{mono|ExecutorService}} instance will be reused under the hood for revenant tasks, so it's possible runs as many concurrent tasks as the programmer wants throughout the life-cycle of the application using a single executor service instance. This is how the first thread-example looks using executors: <syntaxhighlight lang="Java" line> ExecutorService executor = Executors.newSingleThreadExecutor(); executor.submit(() -> { var threadName = Thread.currentThread().getName(); System.out.println("Hello " + threadName); }); </syntaxhighlight> The {{mono|ExecutorService}} instance also supports a {{mono|Callable}} interface, another single method interface like {{mono|Runnable}} but the signature of the contained method of {{mono|Callable}} returns a value. In this way, the lambda expression must also return a value, like the below example of calling a website asynchronously as the C# example. <syntaxhighlight lang="Java" line> ExecutorService executor = Executors.newSingleThreadExecutor(); Future<String> contentAsync = executor.submit(() -> { HttpRequest httpReq = HttpRequest.newBuilder() .uri(new URI("www.graalvm.org")) .build(); return HttpClient.newHttpClient() .send(httpReq, BodyHandlers.ofString()) .body(); }); var webPageResult = contentAsync.get(); </syntaxhighlight> Calling the method {{code|get()}} blocks the current thread and waits until the callable completes before returning the value (in the example, a web page content): The following example, a method and a class are used. This wrap it's just to be similar to the C# example since Java does not have keywords like async for the method signature. <syntaxhighlight lang="Java" line> public static class SomeAsyncCode { static ExecutorService executor = Executors.newSingleThreadExecutor(); public static Future<String> getContentAsync(){ return executor.submit(() -> { HttpRequest httpReq = HttpRequest.newBuilder() .uri(new URI("www.graalvm.org")) .build(); return HttpClient.newHttpClient() .send(httpReq, BodyHandlers.ofString()) .body(); }); } } var webPageResult = SomeAsyncCode.getContentAsync().get(); </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)