One, the creation of threads
1. by inheriting the thread class, overriding the thread's run () method, placing the logic in which the threads run
2. Instantiate the thread class by implementing the Runnable interface
However, there is no start () method in a subclass defined with runnable, only in the thread class. At this point the thread class is observed, and there is a constructor method: Public Thread (Runnable Targer) This construction method accepts the subclass instance of Runnable, which means that the thread class can be used to start the multithreading of the Runnable implementation. (Start () to coordinate the resources of the system)
The difference between the two ways of implementation and contact:
In the development of the program as long as the multithreading must always be implemented runnable interface mainly, because the implementation of the Runnable interface compared to inherit the thread class has the following advantages:
To avoid the limitations of point inheritance, a class can inherit multiple interfaces.
Resource-friendly sharing
second, the life cycle of the thread
Start thread publicvoid start ();p ublicvoid Run ();//suspend and Wake thread publicvoid resume (); The use of publicvoid suspend () is not recommended; It is not recommended to use publicstaticvoid sleep (long Millis);p ublicstaticvoid sleep (long millis, int nanos);//terminating thread publicvoid stop (); It is not recommended to use publicvoid interrupt ();//Get Thread state Publicboolean isAlive ();p Ublicboolean isinterrupted ();p Ublicstaticboolean in Terrupted ();//Join Method Publicvoid Join () throws interruptedexception;
1. Call Thread.Sleep (): Causes the current thread to sleep at least a few milliseconds (although it may be interrupted before the specified time).
2. Call Thread.yield (): There is no guarantee for too many things, although it usually brings the current running thread back to the operational state, allowing threads with the same priority to execute.
3. Call the Join () method: Ensure that the current thread stops executing until the thread to which the thread is joined is complete. However, if the thread it joins does not survive, the current thread does not need to stop.
Third, thread synchronization
1 , thread synchronization is designed to protect the resources when multiple threads ask for a resource.
2 , thread synchronization methods are implemented by locks, each with a single lock, which is associated with a particular object, and once the thread acquires an object lock, other threads that access the object can no longer access other synchronization methods for that object.
3 , for the static synchronization method, the lock is for this class, the lock object is the class 's Class object. Static and non-static methods of locking do not interfere. A thread acquires the lock, which is obtained when a synchronization method on another object is accessed in a synchronous method.
4 , for synchronization, it is critical to be awake at all times on which object to synchronize.
5 , thread-safe classes need to be aware of the logic and security of multiple threads competing for access to resources to make correct judgments about " Atoms " operation to make an analysis and to ensure that other threads during atomic operations cannot access competing resources.
6 , when multiple threads wait for an object lock, the thread that does not acquire the lock will block.
7 , deadlocks are caused by waiting for locks between threads, and the probability of occurring in practice is very small. Really let you write a deadlock program, not necessarily good, hehe. However, once the program has a deadlock, the program will die.
Iv. Some FAQs
1 , the name of the thread, a running thread always has a name, the name has two sources, one is the name of the virtual machine itself, and one is your own set name. In the absence of a specified thread name, the virtual machine always assigns a name to the thread, and the main thread's name is always mian, and the name of the non-main thread is indeterminate.
2 , the thread can set the name, can also get the name of the thread, even the main course is no exception.
3 , the method that gets the object of the current thread is: Thread.CurrentThread () ;
4 , in the code above, you can only guarantee that each thread will start and each thread will run until it is complete. A series of threads starting in some order does not mean that they will be executed in that order. For any set of startup threads, the scheduler does not guarantee its execution order and the duration is not guaranteed.
5 , when the thread target Run () the thread finishes at the end of the method.
6 , once the thread is started, it can never be restarted again. Only one new thread can be started, and only once. A running thread or a dead thread can be restarted.
7 , the scheduling of threads is JVM A part of that in a CPU machine, you can actually run only one thread at a time. Only one thread stack executes at a time. the JVM thread Scheduler determines which threads are actually running in a running state.
One of the many running threads can be selected as the current thread. The order in which the running threads are selected to run is not guaranteed.
8 , although usually in the form of queues, this is not guaranteed. The queue form is when a thread completes a " round " , it moves to the tail of the running queue until it finally queues up to the front end of the queue before it can be selected again. In fact, we call it a running pool instead of a running queue, to help realize that threads are not all in a guaranteed order to sing a queue.
9 , although we have no control over the thread scheduler, there are other ways to influence how threads are dispatched.
Java Multithreaded Learning Notes