And the relationship between the threads:
Process: process is a dynamic process of execution of a program, he manager of the code load, execution to complete the completion of a process, the process itself from the production, development to the final extinction process.
Threads: threads are an effective means of implementing concurrency mechanisms, and processes and threads are a basic unit of concurrency.
Multithreaded implementations in Java:
Integration of thread and implementation runnable
Thread principal content inside threads is executed concurrently through the start method of the thread class.
Runnable threads are executed in the following ways:
Thread is a subclass of the Runnable interface.
The difference between the thread class and the Runnable interface:
1 using the thread class in the operation of multi-threading can not achieve the purpose of resource sharing, runnable interface can achieve resource sharing;
The 2 runnable interface has the following obvious advantages over inheriting the thread class:
Thread State:
Creation state: Thread object creation: Threads t = new thread ();
Ready state: T.start (), call the Start method, wait for the CPU to dispatch;
Running state: Executes the Run method inside the thread;
Blocking state: Temporarily stop execution, may give resources to other threads to use, call the thread's wait or sleep method;
Termination Status: The thread is not in use after it has finished executing.
Thread common methods of operation:
Get the thread and set the thread name:
Thread.CurrentThread (). GetName (); --> Gets the thread name.
Of course, it can be set by the SetName method.
Java runtime starts several threads:
2 articles;
1 JVM threads, one GC thread.
To determine if a thread is running:
Force thread Execution:
The join () method of the calling thread, the other thread cannot run, and must wait for the thread to finish before it can continue execution.
Thread hibernation:
Thread.Sleep ().
Thread Break:
One thread interrupts the state of its operation by another thread, which is done using the Interupt () method.
Background thread:
As long as the program does not finish, the Java process does not disappear, setting the background thread, even if the process disappears, the thread continues to execute.
Implemented by Setdaemon ().
Thread Priority:
Thread comity:
Sync and Deadlock:
Synchronization is what solves the problem, when a data is read and other threads access the thread, then synchronization should be used.
Synchronizing code blocks:
Synchronization method:
Producer Consumer issues:
Thread life cycle:
Threads are:
Suspend () temporarily suspend thread
Resume () resumes a suspended thread
Stop () stopping the thread
These three methods are prone to deadlock problems and are not recommended for use.
A better way to stop a thread:
Let the thread stop by the flag bit.
Java Review-Multithreading