Java source code is compiled into bytecode. Bytecode, when run, is executed by the processor. Sequences of bytecode go through processors, and on occasion these sequences must be analyzed for developing new Java software. Java Enterprise Edition (the programming language of choice for many businesses) is owned by Oracle. However, Oracle is not always the best creator or custodian of documentation for Java. Here is an example (as of November 2015) taken from Oracle’s website: “A thread is a thread of execution in a program." This circular definition does little to help elucidate what a thread is. Techopedia has a more meaningful definition for someone who doesn’t know what a Java thread is: “A thread, in the context of Java, is the path followed when executing a program." This is not a directory path for an instruction set but a path through a processor for an instruction set. An instruction set is a sequence of atomic commands. These commands manipulate data through the processor. The processor stores the data in its registers in the CPU.
According to page 1269 of A Practical Guide to Fedora and Red Hat Enterprise Linux, the term "thread-safe" is another term for "reentrant." This is "[c]ode that can have multiple simultaneous, interleaved, or nested invocations that do not interfere with one another." So non-interference is a key principle. To learn more about threads as opposed to processes, see this Python posting.
Every thread in Java is created and controlled by a unique object of the java.lang.Thread class. When a standalone application is run, a user thread is automatically created to execute the main() method. This thread is called the main thread. In Java, we can implement threads in one of two ways: - By implementing the java.ang.Runnable interface. - By extending the java.lang.Thread class.The above quote was taken from page 174 of Cracking the Coding Interview.
We find this definition of a thread to be excellent, that was taken from this StackOverflow.com posting.:
'A thread is an independent set of values for the processor registers (for a single core). Since this includes the Instruction Pointer (aka Program Counter), it controls what executes in what order. It also includes the Stack Pointer, which had better point to a unique area of memory for each thread or else they will interfere with each other.
Threads are the software unit affected by control flow (function call, loop, goto), because those instructions operate on the Instruction Pointer, and that belongs to a particular thread. Threads are often scheduled according to some prioritization scheme (although it's possible to design a system with one thread per processor core, in which case every thread is always running and no scheduling is needed).
In fact the value of the Instruction Pointer and the instruction stored at that location is sufficient to determine a new value for the Instruction Pointer. For most instructions, this simply advances the IP by the size of the instruction, but control flow instructions change the IP in other, predictable ways. The sequence of values the IP takes on forms a path of execution weaving through the program code, giving rise to the name "thread".'
Once you know what a thread is, you can comprehend Java’s multithreading capabilities. Java can facilitate the concurrent execution of different processes. When these processes consume or edit shared resources, unexpected problems can occur. To prevent these potential problems, the synchronized keyword in a Java program (the source code) can be used. It is helpful for coordinating multithreaded applications. This way when more than one thread is active, data stored in registers is not modified by different threads.
If a Java program is thread-safe, it may have simultaneous parts that operate on shared data but the program will not cause corruption because the different threads will not be in conflict when the program runs (concepts taken from another StackOverflow.com posting).
To learn more about Java thread dumps, see this posting.
To learn more about a thread, see these external postings: