1: Processes and Threads
Process: Runs a program in which the program allocates the running space in memory. A process was started in memory.
Threads: Running in a process. Multiple threads can be switched between each other.
Main thread
Child Threads: Child threads are assigned from the main thread.
2: Importance of the main thread.
3: Creating Multithreading in Java
1: Inherit the Thread class:
2: Implement Runable Interface:
The difference between inheriting the thread class and implementing the Runable interface:
Java only supports single inheritance, and once a class inherits the thread class, it can no longer inherit other classes.
4:seleep method
5:thread.join method: If a thread joins another thread, the thread must wait for another thread
The Run method runs end (thread end). Method of adjusting = =
Thread.yield method: Give up the CPU resources, let the other threads execute.
Thread.setpriority (): Sets the priority of the thread. Priority, the longer the CPU is scheduled.
Max = 10
Normal = 5
Min = 0;
The value range is between 0---10.
Thread.seleep (): The Sleep of the thread. Unlike yield, it is dormant. Can be awakened.
Methods to stop a thread:
The old way of realizing it before
Stop/destory method. Causes deadlocks (to understand deadlocks in a way that combines iterators and collections)
Now the new way:
6: Why to use thread synchronization:
A: When multiple threads access a resource at the same time, it causes the resource data to be out of sync. Therefore, the resources are locked.
The lock-up process is that only one thread can access the resource when the threads are concurrent. So called
Synchronization of Threads.
7: The concept of the lock
8: What exactly locks:
Locks the object and class where the current method (current variable) resides.
Synchronized (synchronous)
Asynchronized (asynchronous)
9: How to Implement Thread synchronization:
A: Method Synchronization.
B: code block synchronization:
Method synchronization differs from code block synchronization:
Method synchronization: Locks the object on which the current method resides. Therefore, the entire method is executed until it is complete, locking the current object.
Code block synchronization: The lock on an object is only a block of code that executes the synchronized part. Other blocks of code will also appear
Current thread synchronization issues.
Synchronized (this) {
Count = count + 1;
try {
Thread.Sleep (2000);
} catch (Exception e) {
E.printstacktrace ();
}
System.out.println ("Current thread =" + Thread.CurrentThread (). GetName ()
+ ", count value =" + count);
}
System.out.println (count);
10: Coarse grain size of the lock:
The coarse granularity of the lock can only change the size of the lock in code block synchronization. There is no way to change the method's synchronization.
11: Collaboration between Threads:
Wait ();/notify ();
J2SE Basics: 13. Multithreaded programming