1 class Thread1 extends Thread
Thread1 mth1=new Thread1 ("A");
Thread1 mth2=new Thread1 ("B");
Mth1.start ();
Mth2.start ();
2 class Thread2 implements Runnable
New Thread (New Thread2 ("C")). Start ();
New Thread (New Thread2 ("D")). Start ();
The benefits of implementing the Runnable interface are more than inheriting the thread class:
1): Suitable for multiple threads of the same program code to process the same resource
2): Can avoid the restriction of single inheritance in Java
3): Increase the robustness of the program, the code can be shared by multiple threads, code and data Independent
1. New state: A new Thread object was created. 2. Ready state (Runnable): After the thread object is created, other threads call the object's start () method. The state of the thread is located in a pool of running threads that becomes operational and waits for the CPU to be used. 3, running State (Running): The ready state of the thread gets the CPU, executes the program code. 4, blocking State (Blocked): Blocking state is the thread for some reason to abandon the use of the CPU, temporarily stop running. Until the thread is in a ready state, the opportunity to go to the running state is reached. There are three types of blocking: (i), waiting for blocking: The running thread executes the wait () method, and the JVM puts the thread into the waiting pool. (ii), synchronous blocking: When a running thread acquires a synchronization lock on an object, the JVM puts the thread into the lock pool if the synchronization lock is occupied by another thread. (iii), other blocking: The running thread executes the sleep () or join () method, or when an I/O request is made, the JVM will place the thread in a blocked state. When the sleep () state times out, join () waits for the thread to terminate or time out, or the I/O process finishes, the thread is re-entered in a ready state. 5. Dead State (Dead): The thread finishes executing or exits the run () method because of an exception, and the thread ends the life cycle. 1, thread prioritization: Java threads have priority, and high-priority threads get more running opportunities. The priority of a java thread is expressed in integers, and the value range is the following three static constants for the 1~10,thread class: Static int max_priority The thread can have the highest priority, with a value of 10. The static int min_priority thread can have the lowest priority, with a value of 1. The static int norm_priority The default priority assigned to the thread, with a value of 5. The SetPriority () and GetPriority () methods of the thread class are used to set and get the priority of the thread, respectively. Each thread has a default priority. The default priority for the primary thread is thread.norm_priority. The priority of a thread has an inheritance relationship, such as the B thread created in a thread, then B will have the samePriority level. The JVM provides 10 thread priorities, but does not map well with common operating systems. If you want the program to be portable to each operating system, you should only use the thread class with the following three static constants as priority, which ensures that the same precedence is used for the same scheduling.  2, Thread sleep: thread.sleep (Long Millis) method, which causes the thread to go to the blocking state. The Millis parameter sets the time to sleep, in milliseconds. When sleep is over, it becomes ready (Runnable) state. Sleep () platform portability is good. 3, thread wait: The Wait () method in the object class causes the current thread to wait until the other thread calls the Notify () method of this object or the Notifyall () Wake method. This two wake-up method is also a method in the object class, and behaves equivalent to calling wait (0). 4, thread Concession: the Thread.yield () method pauses the currently executing thread object, giving the execution opportunity to the same or higher priority thread. 5, thread join: Join () method, wait for other threads to terminate. Invoking the Join () method of another thread in the current thread, the current thread goes into a blocking state until the other process finishes running, and the current thread is then turned into a ready state by blocking. 6, Thread Wake: The Notify () method in the object class, which wakes up a single thread waiting on this objects monitor. If all threads are waiting on this object, then one of the threads is chosen to wake up. The choice is arbitrary and occurs when a decision is made on the implementation. The thread waits on the object's monitor by calling one of the wait methods. Until the current thread discards the lock on this object, it can continue to execute the awakened thread. The awakened thread competes with all other threads that are actively synchronizing on the object, for example, the thread that wakes up does not have a reliable privilege or disadvantage as the next thread that locks the object. A similar approach also has a notifyall () that wakes up all the threads waiting on this object monitor. the difference between sleep () and yield ()
The difference between sleep () and yield (): sleep () causes the current thread to stall, so the thread executing sleep () is definitely not executed for the specified time, and yield () simply returns the current thread back to the executable state, so yield () is executed Threads are likely to be executed immediately after they have entered the executable state.
The sleep method makes the current running thread sleepy for a period of time, into a non-operational state, the length of time is set by the program, the yield method makes the current thread out of the CPU possession, but the time is not set. In fact, the yield () method corresponds to the following actions: first detect whether the thread with the same priority is in the same operational state, and if so, hand over the CPU's possession to this thread, or continue running the original thread. So the yield () method is called "concession," which gives the opportunity to other threads of equal priority.
In addition, the sleep method allows a lower-priority thread to get a run-time, but when the yield () method executes, the current thread is still in a running state, so it is not possible to get CPU possession when a lower-priority thread is given. In a running system, if a higher-priority thread does not call the sleep method and is not blocked by i\o, the lower-priority thread can only wait for all higher-priority threads to run to the end before the opportunity runs.
Sleep (): Forces a thread to sleep n milliseconds.
IsAlive (): Determines whether a thread is alive.
Join (): Waits for the thread to terminate.
Activecount (): The number of active threads in the program.
Enumerate (): Enumerates the threads in the program.
CurrentThread (): Gets the current thread.
Isdaemon (): Whether a thread is a daemon thread.
Setdaemon (): Sets a thread as the daemon thread. (The difference between a user thread and a daemon thread is whether to wait for the main thread to end up relying on the main thread end)
SetName (): Sets a name for the thread.
Wait (): Forces a thread to wait.
Notify (): Notifies a thread to continue running.
SetPriority (): Sets the priority of a thread.
Java Multithreading (thread/runnable)