1, the creation of multi-threaded method One: 1, custom inherits from the thread of the subclass; 2, rewrite run (), custom code add 3, create a Thead subclass object, and call the start () method to open the thread (when the thread is turned on, it executes the RU n method, do not call the run () method directly) method two: (recommended) Java is a single inheritance, multi-Implementation (interface) 1, custom class implementation runnable interface 2, the implementation of the Runnable Interface Run (), the task of the custom thread defined in the run () 3. Create a Runnable implementation class object and create an object of the thread class, pass the Runnable object as an argument 4, call the Start method of the thread object to open the threads runnable the object of the implementation class is not a thread object, only Is the object that implements the Runnable interface, Only the thread and its subclasses are the objects of the Runnable implementation class that are passed as arguments to the thread object is the runnable implementation of the object's Run method as the thread's task code execution This method can be used without static decoration to ensure that the same resource is shared. Passed in is the same Runnable class implementation object has been guaranteed 2, thread common methods
(1)
Thread (String name); // initializes the thread name, and the thread's Run method does not throw an exception type, so the subclass cannot be thrown and can only capture
(2)
GetName (); // returns the name of the thread
(3)
SetName (String name); // changing the name of a thread
(4)
Sleep (long millis); // static method, which thread executes which thread is sleeping
(5)
GetPriority (); // the priority of the thread is 5 by default
(6)
SetPriority (int newpriority); // Priority 1~10
(7)
CurrentThread (); // returns a reference to the currently executing object, which thread executed the object that returned which thread
Non-static member variables maintain one copy of the data in each object
3. Thread Safety
Multiple objects share the same resource
Solution: Thread Synchronizationmode one: Synchronizing code blocks (recommended)synchronized (lock object) {Sync code}The lock object can be any statically unique object, static object o = new Object (); Calling sleep in a synchronous code block does not release the lock object the synchronization mechanism uses the state in the object as the flag of the lock (0 (ON)) mode two: The synchronization function modifies a function with synchronized (public static Synchroni zed void Getmoney ()) The lock object of the non-static synchronization function is the this object; The lock object of the static synchronization function is the byte-code file (class object) of the class to which the current function belongs. The lock object of the synchronization function is fixed, and the byte-code file of the class to which the static function belongs cannot be arbitrarily specified. Thread.class, byte-code objects are unique
4, the deadlock synchronization mechanism solves the thread security problem, but also raises the condition that the deadlock exists: 1, there are two or more than two threads 2, there are two or more than two shared resources no solution, try to avoid 5, thread communication
Notifies another thread when a thread finishes a task
Producer and consumer issues
Wait (); // Enter the wait state (in the thread pool where the lock object is an identifier), must be called by another thread notify to wake, release the Lock object notify (); // Notifyall ();
Precautions:
1, all belong to object objects (lock object is any object) 2, must be in a synchronous code block or synchronization function to use (the synchronization code block has lock object, must be called by the lock object) 3, must be called by the lock object (lock object guaranteed wake or wait line the same identifier of one of the threads in the pool) Thread Stop Method 1, generally through a flag variable control Method 2, Interrupt (), forced to clear the wait state, the thread received the Interruptexecption exception daemon thread (background thread) only a daemon thread left in a process, The daemon thread also dies Isdaemon (): Determines whether the daemon thread Setdaemon (): Sets the daemon thread, which is not the Daemon join () By default: Joins a new thread, executes the old thread after the new thread finishes executing
7. Multithreading