There are two ways to implement multithreading in Java: Inheriting the thread class or implementing the Runnable interface. Runnable is an interface, it is recommended to use the interface to generate the thread, because the interface can implement multiple inheritance, and runnable only a run method, is suitable for inheritance. You only have to inherit the thread when using thread, and the new instance comes out and the start () method is called to start a thread.
Thread Test = new Thread ();
Test.start ();
When using runnable, you need to first implement an instance of Runnable, and then start the thread.
Test impelements Runnable;
Test T = new Test ();
Thread test = new Thread (t);
Test.start ();
Summary: Thread and runnable are 2 ways to implement Java Multi-threading, Runable is an interface, thread is a class, it is recommended to use Runable to implement Java multi-threading, regardless, eventually need to pass Thread.Start () To keep the thread in a running state.
2. Know the start and run of thread
1) Start:
Start the thread with the Start method, and actually implement multi-threaded operation, without waiting for the Run method body code to complete and proceed directly to execute the following code. By invoking the start () method of the thread class to start a thread, the thread is in a ready (operational) state and is not running, and once the SPU time slice is obtained, the run () method is executed, where the method run () is called the thread body, which contains the contents of the thread to be executed. This thread terminates when the Run method finishes running.
2) Run:
The run () method is just a common method of the class, if you call the Run method directly, the program is still only the main thread of the threads, its program execution path is only one, or to execute sequentially, or to wait for the Run method body execution before you can continue to execute the following code, This will not achieve the purpose of writing threads.
Summary: Call the Start method to start the thread, and the Run method is just a normal method call to thread, or execute in the main thread.
3. Thread Status Description
Thread state from a large aspect, can be summed up as: the initial state, operational status, non-operational state and extinction state, the specific can be subdivided into the 7 states shown below:
1) The implementation of the thread has two ways, one is to inherit the thread class, and the other is to implement the Runnable interface, but anyway, when we new thread instance, the thread enters the initial state;
2) When the object calls the start () method, it enters the operational state;
3) After entering the operational state, when the object is selected by the operating system, the CPU time slice will go into the running state;
4) After entering the state of operation, case is more, roughly the following situation:
After the. Run () method or the main () method ends, the thread enters the terminating state;
When a thread calls its own sleep () method or another thread's join () method, it goes into a blocking state (which stops the current thread, but does not release the resources it occupies). When sleep () ends or join () ends, the thread enters the operational state and continues to wait for the OS to allocate time slices;
When a thread has just entered the operational state (note that it is not yet running), discovering that the resource to be called is locked (synchroniza,lock), it will immediately enter the lock pool state and wait for the lock tag (at this point the lock pool may already have other threads waiting to acquire the lock tag, when they are in the queue State, On a first-come-first-served basis, once the thread obtains the lock tag, it goes to the operational state, waiting for the OS to allocate CPU time slices;
When a thread calls the Wait () method, it enters the waiting queue (which frees up all the resources it occupies, unlike the blocking state), which is not automatically awakened after it enters this state, and must rely on other threads to invoke the Notify () or Notifyall () method to be awakened ( Since notify () only wakes up a thread, we are not sure which thread is specifically awakened, perhaps the thread we need to wake up is not able to wake up, so in practice it is generally used with the Notifyall () method, which wakes up some threads), and the thread is awakened and enters the lock pool. Wait for the lock token to be acquired.
When a thread calls the Stop method, the thread goes extinct, but because the stop method is unsafe and discouraged, you can implement the thread stop by using the conditions in the Run method.
Threading creation and use in Java