The most important idea in Java: a thread is a different execution path within a program
2. Example: see only one path below, main () is just the main process, Process execution: the main thread in the process begins; Process vs Threading Concept
Threads are small units of process, I. There is at least one process during program execution, at least one thread in a process, and multiple concurrent threads in a process.
II. Threads: The Main method in the process, the threads are the different paths that the process executes, so the thread change will only represent the change of the CPU's execution process
III. A CPU will only execute one thread at a time, Multithreading: The CPU is dual-core,
3.
1 PackageTestthread;2 3 Public classTestThread1 {4 5 Public Static voidMain (string[] args) {6 //TODO auto-generated Method Stub7Runner1 r=NewRunner1 ();8 //implement the Runnable interface by defining the thread class, passing in thread (target), defining the9 //class in the Override Run methodTenThread t=NewThread (r); OneT.start ();//start a thread, and the main () will go down automatically A - for(inti=1;i<100;i++) - { the -System.out.println ("Start Thread ..." +i); - } - + } - + } A //The Run method then a method of the thread class, using the interface concept, the Implements method at classRunner1Implementsrunnable{ - Public voidrun () { - for(inti=0;i<100;i++) - { - -System.out.println ("Runner1:" +i); in } - to } +}
The output in this case is the first output of the value in the main () method, the value in the output runnable
Using inherited methods, it is generally better to use an interface as a thread
1 PackageTestthread;2 3 Public classTestThread1 {4 5 Public Static voidMain (string[] args) {6 //TODO auto-generated Method Stub7Runner1 r=NewRunner1 ();8 //implement the Runnable interface by defining the thread class, passing in thread (target), defining the9 //class in the Override Run methodTen //thread t= new Thread (r); One //T.start ();//start a thread, and the main () will go down automatically A R.start (); - for(inti=1;i<100;i++) - { the -System.out.println ("Start Thread ..." +i); - } - + } - + } A //The Run method then a method of the thread class, using the interface concept, the Implements method at classRunner1extendsthread{ - Public voidrun () { - for(inti=0;i<100;i++) - { - -System.out.println ("Runner1:" +i); in } - to } +}
4. Thread state transitions:
1 PackageTestthread;2 ImportJava.lang.Thread;3 4 Public classThreadjoin {5 Public Static voidMain (string[] args) {6MyThread2 t1=NewMyThread2 ("ABCD");7 //Open Thread8 T1.start ();9 Try{TenT1.join ();//The original process in the T1.start () process will have a branching process, through the T1.join method, the branching process into main main process One //frees up a thread, originally two methods will automatically output one, become a process A - } - Catch(Interruptedexception e) {} the for(inti=1;i<=10;i++) - { -System.out.println ("I am Main thread"); - } + - + } A at } - - classMyThread2extendsthread{ - MyThread2 (String s) - { - Super(s); in } - Public voidRun () to{ for(inti=1;i<=10;i++) + { -System.out.println ("I OUs" +getName ()); the Try * { $Sleep (1000) ;Panax Notoginseng } - Catch(interruptedexception e) the { + return; A } the } + } - $ $}
6. Thread Priority level
Thread T1=new Thread (new T1 ())
T1.setproiority (thread.norm_priority=3)
To automatically stop the threading method:
Java (multithreaded Analysis)