开发者

Thread at language level

开发者 https://www.devze.com 2023-01-08 03:48 出处:网络
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.

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 th开发者_如何学Pythonat support thread at language level...


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.


There is java.lang.Thread and java.util.concurrent, which contains some sugar like semaphors, executors, blockingQueues etc.
Seems like (i don;t know for sure) that all the classes in util.concurrent are based on java.lang.Thread, so java.lang.Thread is as low-level as you can get in java. Meaning that java.util.concurrent is high-level comparing to Thread.


It means that it has a programmer-friendly abstraction over threads (see java.lang.Thread). It supports simple yet powerful synchronization with synchronizedkeyword. It also has great utilities for thread pooling, concurrency, locking etc. in java.util.concurrent.


That means threads are part of the Java language specification as opposed to thread libraries in C, for example.


AFAIK synchronized and volatile are the only concurrent language features present in Java. Everything else is just a library of wrappers for common synchronization mechanisms (semaphores, locks, mutexes, etc).


There is a fairly long chapter in the Java Language Specification - a strong indicator, that threading is supported through by the language itself. The most obvious Java language elements are the keyword synchronized and volatile. Another supporting element is the special effect of the Thread class:

Threads are represented by the Thread class. The only way for a user to create a thread is to create an object of this class; each thread is associated with such an object. A thread will start when the start() method is invoked on the corresponding Thread object.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号