1. Concept
Because Java is completely object-oriented, in Java, we are talking about a thread that is an instance object of the thread class. So, a thread is an object, it has its own fields and methods.
2. Creating Threads
There are 2 ways to create a thread: 1, the thread class of the extended threading parent class, and 2, the implementation of the Runnable interface. 2 methods are differentiated by.
tip : The thread class has implemented the Runnable interface, and the thread class not only implements the Run method, but also defines a more comprehensive approach to threading the object, while the Runnable interface has only the Run method.
By extending the thread class
Public classTest { Public Static voidMain (string[] args) {System.out.println ("I Am" +thread.currentthread (). GetName () + Threads); MyThread T1=NewMyThread ("Thread 1"); MyThread T2=NewMyThread ("Thread 2"); T1.start (); T2.start (); System.out.println (Thread.CurrentThread (). GetName ()+ "Thread run Complete"); } }classMyThreadextendsthread{ PublicMyThread () {Super(); } PublicMyThread (String name) {Super(name); } @Override//overriding the Run method of the parent class Public voidrun () { for(inti=0;i<5;++i) {System.out.println ("I am" + This. GetName ()); Try{Thread.Sleep (200); } Catch(interruptedexception e) {e.printstacktrace (); } } } }
Output:
I am the main thread
Main thread finished running
I am a thread 2
I am a thread 1
I am a thread 1
I am a thread 2
I am a thread 1
I am a thread 2
I am a thread 1
I am a thread 2
I am a thread 2
I am a thread 1
Summary method found : 3 threads Preempt CPU resources, quickly alternating execution, who also waits for who, they only wait CPU.
By implementing the Runnable interface
Public classTest { Public Static voidMain (string[] args) {mythreadtest com=Newmythreadtest (); Thread T1=NewThread (COM, "Threads 1")); Thread T2=NewThread (COM, "Threads 2")); T1.start (); T2.start (); } }classMythreadtestImplementsrunnable{Private intnum = 50; @Override Public voidrun () { while(num>=1) {System.out.println ("From" +thread.currentthread (). GetName () + "num =" +num); Num--; } } }
Output: (Different machine and run time will output various results)
From thread 2 num = 10
From thread 1 num = 10
From thread 2 num = 9
From thread 1 num = 8
From thread 2 num = 7
From thread 1 num = 6
From thread 2 num = 5
From thread 1 num = 4
From thread 1 num = 2
From thread 1 num = 1
From thread 2 num = 3
We found that the number of threads 1 and 2 output is not complete from 10 to, because they 2 programs together operate a "pseudo-threading class"mythreadtest instance, is COM. So, when the thread T1 the value of NUM to 9, the value of num T2 accessed by this time is 9. This involves thread synchronization , and I'll explain it in a later essay.
Why do I call mythreadtest "pseudo-threading class"? Because the Mythreadtest class is not a thread class at all. It and thread both implement the Runnable interface, but mythreadtest and thread do not have an inheritance relationship, as we said earlier,threads in Java are an instance of the thread class . Like the rectangle class and the triangle class both implement the shape interface, can a triangle be seen as a rectangle? Obviously, it's not possible.
So, we're going to create threads through the Runnable interface, or we need the thread class.
thread T1 = new Thread (COM);
Thread t2 = new Thread (COM);
Because T1 and T2 are constructed using the same "pseudo-Threading Class" Object COM, T1 T2 operates on the same object com. If you don't want to, then provide different "pseudo-threading Class" Objects for T1 T2, respectively.
thread T1 = new Thread (new Mythreadtest ());
Thread t2 = new Thread (new Mythreadtest ());
Summary
First, generally we recommend the implementation of runnable, although more than one line of code, but this more flexible, strong extensibility.
The Run method in a custom thread is equivalent to the main method in the main thread, why is it named Main? Because this is the contract between Java and the system, main is the signature of the program entrance, and he is a flag, when the Java program starts, it will call the method named Main. In the same vein, a new thread will execute its business logic code and need a well-appointed name: Run. It is no different from other methods except that it has a special name.
3, the thread class in some commonly used threading methods (marked with * is to focus on the method)
*t.start () |
To get the t thread into a ready state, with the right to be executed by the CPU, but not necessarily immediately. |
*t.run () |
Do not display the call to the Run method yourself, and the Run method executes automatically when the CPU gives a thread an opportunity to execute. |
T.getname () |
Returns the name of this thread |
T.setname (String name) |
Set the name of the thread |
t.setpriority (int level) |
Set the priority of a thread Thread. max_priority Highest priority, 10 Thread. min_priority Lowest Priority 1 Thread. Norm_priority General priority 5 After the priority is set, the JVM's event scheduler invokes the thread sequentially, based on the priority of the thread. Usually high-priority threads run ahead of low priority, but this rule is not always true. Priority will only change the thread who starts first, and who finally starts. The CPU is fixed on a single execution time slice for each thread. |
T.getpriority () |
Gets the priority of the thread |
*t.yield () |
The thread actively abandons the chance of being executed by the CPU and then goes into the ready state again, and then executes it next time. |
*t.join () |
Thread merge. This sentence is written in which thread, which is to let the T1 thread into the execution process of that thread. The thread that executes this sentence is paused until the T1 thread finishes executing, and then executes. For example, in the main thread there is T1.join (), and when the join function is executed, the main thread is held until the T1 thread finishes executing, and then it returns to main. |
*t.isalive () |
Whether the thread is alive |
|
|
|
|
|
|
static method: |
|
Thread.CurrentThread () |
Reverse the reference to the currently running thread |
*thread.sleep (Long Millis) |
Let a thread sleep Millis milliseconds |
Not finished ... Pending additions |
|
Java multithreaded programming (two create Threads)