You are welcome to access my Baidu Library because of the limited number of characters in the space and the inconvenience of sending images!
Http://wenku.baidu.com/view/9a02cb02de80d4d8d15a4f57.html
Chapter 9 basic concepts of thread 9.1
1. A thread is the sequential control flow within a program (a thread is a different execution path in a program ).
2. Differences between threads and processes
A) each process has an independent code and data space, and switching between processes has a high overhead.
B) a thread can be considered as a lightweight process. The same class of threads share code and data space. Each thread has an independent running stack and program counter (PC), and the thread switching overhead is small.
C) multi-process: multiple tasks (programs) can be run simultaneously in the operating system ).
D) multithreading: Multiple Threads can be executed at the same time in the same application.
3. Java threads are implemented through the JVA. Lang. Thread class.
4. When the VM is started, there will be a thread defined by the main method (public static void main.
5. You can create a thread by creating a thread instance.
6. Each thread performs operations through the method run () corresponding to a specific thread object. The method run () is called the thread body.
7. Call the START () method of the thread class to start a thread.
8. Thread writing
Class runner1 implements runnable {// the runnable interface is recommended.
// Class runner1 extends thread {// inheritance is not recommended!
Public void run (){
For (INT I = 0; I <100; I ++ ){
System. Out. println ("runner1:" + I );
}
}
}
9. The thread call can be started using the run method, but this is the call of the method, rather than multithreading!
The real thing is to call the START () method of thread!
Public class testthread1 {
Public static void main (string [] ARGs ){
Runner1 r = new runner1 ();
// R. Run (); // This is the method call, not the real multithreading.
Thread t = new thread (R );
T. Start ();
For (INT I = 0; I <100; I ++ ){
System. Out. println ("main thread: ------" + I );
}
}
}
10. Thread creation and startup
First:
1) define the field class to implement the runnable interface
2) thread mythread = new thread (target); // The target is of the runnable interface type.
3) There is only one method in runnable:
Public void run (); used to define the running body of a thread
4) use the runnable interface to provide data sharing for multiple threads
5) The thread static method can be used in the run method definition of the class implementing the runnable interface.
Public static thread currentthread () reads the reference of the current thread
Second:
1) You can pin a thread and overwrite its run method, for example:
Class mythread extends thread {
Public void run (){...}
}
2) then generate the object of this class
Mythread = new mythread (...)
Testthread1.java
Public class testthread1 {
Public static void main (string [] ARGs ){
Runner1 r = new runner1 ();
// R. Run ();
Thread t = new thread (R );
T. Start ();
For (INT I = 0; I <100; I ++ ){
// Thread. Sleep (1 );
System. Out. println ("main thread: ------" + I );
}
}
}
// Class runner1 implements runnable {
Class runner1 extends thread {
Public void run () {// The method to be rewritten cannot throw an exception that is different from the method to be overwritten.
For (INT I = 0; I <100; I ++ ){
// Thread. Sleep (1 );
System. Out. println ("runner1:" + I );
}
}
}
Please go to Baidu Library to view the following content!