The operating system can produce multiple processes, and each process can produce multiple threads
1. Threads
"Main thread" is main threads
Threads have 4 states: New, run, Interrupted, dead
(1) NEW:
① is created by inheriting the Thread class:
The Run () method of the Thread class needs to be rewritten;
Pros: You can add new member variables and methods to the subclass so that the thread has some kind of property and functionality.
Disadvantage: Java does not support multiple inheritance, and subclasses of the thread class can no longer extend other classes.
② is created by implementing the Runnable interface:
Construction method:Thread (Runnable target)
Advantage: For threads that use the same target object, the member variables of the target object are the data units shared by those threads;
The class that creates the target object can be a subclass of a particular class, if necessary.
Therefore, using the Runnable interface is more flexible than subclasses using thread.
Reason for interruption:
①JVM switches the CPU resources from the current thread to other threads, leaving this thread out of the CPU's right of use in a broken state.
② thread uses the CPU resources, the sleep (int millsecond) method is executed to put the current thread into hibernation.
③ threads use CPU resources, the Wait () method is executed to allow the current thread to enter a wait state.
The ④ thread uses CPU resources to perform an operation that goes into a blocking state, such as performing a read/write operation that causes blocking.
Death (thread frees the memory of the entity and thread objects) cause:
① finishes executing all the statements in the run () method, ending the Run () method.
The ② thread is terminated prematurely, forcing the run () method to end.
The relationship of the target object to the thread:
① fully decoupled: The target object does not have a composite thread object.
② weak coupling: The target object can combine threads, threading the thread as its own member (creating threads in the thread subclass or class that implements Runnable).
In the actual problem, according to the actual situation to determine the target object and the thread is a combination or complete decoupling relationship, the two relationships have advantages and disadvantages.
Common Methods for threading:
1.start () throws Illegalthreadstateexception
Starting the thread, two calls to the start () method can cause an exception;
2.Run ()
Defines the operation that the thread object executes after it is dispatched, which is automatically invoked by the system and cannot be referenced by the user program;
3.sleep (int millsecond) throws Interruptedexception
The parameter millsecond is the sleep time in milliseconds, and if the thread is interrupted during hibernation, the JVM throws an exception;
4.isAlive ()
Determine if a thread exists (not new and dead);
5.CurrentThread ()
Returns the thread that is currently using CPU resources;
6.Interrupt ()
"Wakes up" the dormant thread, causing the dormant thread to have a interruptedexception exception;
2. Thread synchronization (modified with synchronized)
Thread synchronization mechanism: When a thread A uses the Synchronized method, other threads must wait until thread a uses the synchronized method when they want to use the Synchronized method.
Methods in the synchronization method (all the final methods of the object class):
1.Wait ()
In the execution of the disconnection process, so that the thread waits, temporarily give up the use of the CPU;
2.Notify ()
Notifies a thread that is waiting for the end of a wait;
3.Notifyall ()
Notifies all threads that have been waiting for this synchronization method to end the wait, and the thread that was interrupted continues to execute the synchronization method from the point where it was just interrupted;
The above three methods cannot be used in a non-synchronous method
Follow the principle of "break first, go ahead"
3. Thread Federation
While a thread A is occupying CPU resources, it is possible for other threads to call the join () method and this thread union, such as: B.join ();
If thread A is federating a B thread during the time it occupies CPU resources, the A thread will immediately break execution until it has finished executing the B thread, and a thread is re-queued for CPU resources in order to resume execution.
If a ready-to-federate B-thread has ended, then B.join () will have no effect.
4.GUI Threads
When a Java program contains a GUI, the JVM automatically launches more threads when it runs the program, with two important threads:awt-eventquecue and Awt-windows
When the ActionEvent event is triggered, the awt-eventquecue thread waits for the code that handles the event to be executed immediately.
5. Timer thread
Javax.swing.Timer Class (avoid confusion with the Java.util.Timer Class):
Construction Method:
Timer (int a)
You must call the addActionListener (ActionListener Listener) method to get the monitor (must be a component class);
Timer (int a,object b)
The unit of parameter A is milliseconds, the timer is determined every a millisecond "ringing" once, the parameter B is the monitor of the timer;
Common methods:
1.setreapeats (Boolean B)
Just want the timer to ring once, you can call this method, the parameter B evaluates to false;
2.setinitialdelay (int Depay)
Set the time delay of the first ringing, the default delay is a;
3.Start ()
Start the timer;
4.Stop ()
Stop the timer, that is, suspend the thread;
5.Restart ()
Restart the timer, which is the recovery thread;
6. Daemon Threads
The thread defaults to a non-daemon thread (the user thread), and a thread calls the void Setdaemon (Boolean on) method to set itself as a daemon thread, such as:Thread.setdaemon (true);
When all user threads end up running, the daemon's Run method ends immediately, even if it is not done.
Daemon threads are used to do something that's not very rigorous.
Java Multithreading mechanism