J2SE basics: 13. multi-thread programming and j2se multi-thread programming
1: process and thread
Process: runs a program, and the program allocates runtime space in the memory. A process is started in the memory.
Thread: run in the process. Multiple Threads can switch between each other.
Main thread
Subthreads: subthreads are all allocated from the main thread.
2: The importance of the main thread.
3: Create multithreading in Java
1: Inherit the Thread class:
2: implement the Runable interface:
The differences between inheriting the Thread class and implementing the Runable interface:
Java only supports single inheritance. Once a class inherits the Thread class, it cannot 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 of (the thread ends ). ==== Call Method
Thread. yield method: This method allows other threads to execute CPU resources.
Thread. setPriority (): sets the Thread priority. Priority, the longer the CPU scheduling time.
Max = 10
Normal = 5
Min = 0;
The value range is between 0 and 10.
Thread. seleep (): Sleep Of a Thread. Unlike yield, It is sleep. Can be awakened.
Method for stopping a thread:
Old implementation methods
Stop/destory method. It will lead to deadlocks (understanding deadlocks in combination with the iterator and set)
The new method is as follows:
6: Why use thread synchronization:
A: When multiple threads access A resource at the same time, the resource data is not synchronized. Therefore, you must lock the resource.
This locking process means that only one thread can access this resource during thread concurrency. Therefore
Thread synchronization.
7: Lock Concept
8: What is the lock:
Lock the object and class of the current method (current variable.
Synchronized (synchronous)
Asynchronized (asynchronous)
9: How to Implement thread synchronization:
A: Method synchronization.
B: code block synchronization:
Differences between method synchronization and code block synchronization:
Method synchronization: Lock the object of the current method. Therefore, the current object is locked until the entire method is executed.
Code block synchronization: the object lock is only executed in the synchronized part of the code block. Other code blocks
The current thread synchronization problem.
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 granularity of the lock:
The coarse granularity of a lock can only be changed during code block synchronization. There is no way to change the method for synchronization.
11: collaboration between threads:
Wait ();/notify ();
Multi-thread programming in LINUX
After pthread_create is finished and success is printed, your main thread returns and exits, resulting in the entire process being completed. The newly created thread naturally ends. The solution is to wait for the main thread to exit after the new thread ends, as shown below:
Int tmp = pthread_create (& t_id1, NULL, run1, NULL );
If (tmp = 0)
Cout <"success" <endl;
// Wait t_id1 complete
Pthread_join (t_id1, NULL );
Return 0;
J2se getting started ebook download "the last 256 points for all"
There is a self-learning path on the shangxuetang website.