Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread at language level

I'm reading about threads and in many books is said that Java support threads at language level and at a high-level trough the java.util.concurrent package.

What does it meaning supporting thread at language level?

I think Erlang is a language that support thread at language level...

like image 824
xdevel2000 Avatar asked Aug 31 '26 07:08

xdevel2000


1 Answers

To have threading supported at the language level means that the language provides first-class support for multi-threading, as opposed to just providing second-class support via class libraries.

In Java, threading is supported at the language level with the synchronized and volatile keywords. Using monitors and volatile fields are relatively low-level threading constructs - higher level constructs, such as generic Locks, Barriers, ThreadPools, Concurrent collections are found in the java.util.concurrent package, along with low level atomic operations.

Threading in Java is more than a few keywords in the language. The Java Memory Model mandates the results of multi-threaded memory access, such as when changes by one thread are visible to other threads. This ensures correctly written threaded programs function as intended regardless of the underlying architecture (instruction re-ordering, cache-coherence polices etc..)

The original java class library provides threading support with java.lang.Thread, representing a thread, and since JDk 1.2, java.lang.ThreadLocal, representing thread-local variables. The original JDK also includes an abstract notion of an executable object - java.lang.Runnable. The concurrency utils extend this with Callable and Future, which make creating asynchronous results much simpler than it would be coding with just the low level constructs.

While you can make do with volatile, synchronized and Runnable (as many did prior to JDK 1.5) the classes provided by the concurrency utils make writing threaded programs much easier and with a greater chance of them being correct.

like image 59
mdma Avatar answered Sep 03 '26 07:09

mdma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!