---restore content starts---
Use of Threads:
Thread termination can only be determined by the thread itself, so we use a flag bit while (BOOlean B) in the Run () method to execute the code
void join () or void join (long Millis) assumes that the method of the B thread is called in a thread, and a thread is blocked.
A thread can resume execution only after the B thread has finished executing or when the Millis parameter has been executed
Set Deanon (True) to the daemon thread, when there is no user thread in the process, the virtual machine exits, the default is the user thread
Thread.CurrentThread () Gets the object that represents the current thread
Sleep (long Millis) causes the current thread to hibernate without losing the state of the monitor
GetName () Gets the name of the thread, the main thread whose name is the main child thread is incremented for thread0~n
The benefits of implementing the Runnable interface with respect to inheriting the thread class
1, suitable for multiple threads of the same program code to deal with the same resource situation, the virtual CPU (thread) with the code of the program, the data effectively separated, better reflect the object-oriented design ideas.
2. You can avoid the limitations of the single inheritance feature of Java.
3, in favor of the robustness of the program, code can be shared by multiple threads, code and data is independent. When multiple threads execute code from an instance of the same class, they are said to share the same code. Multiple threads manipulate the same data. When a thread is constructed, it is necessary for code and data to pass through an object as a constructor argument, which is an instance of a class that implements the Runable interface;
Multithreaded instances:
public class Threaddemo {
/**
* @param args
*/
public static void Main (string[] args) {
Mythread my = new Mythread ();//Create a thread target
Thread TR1 = new Thread (my);//Create a thread to execute the thread target's run () method
Thread TR2 = new Thread (my);//Create a second thread, the run () method of the thread target
Tr1.start ();//The Run () method that executes the first thread target
Tr2.start ();//Run () method that executes the second thread target
}
Class Mythread implements runnable{
The properties of the private int J =0;//thread Target are shared by the run () method of the different threads
@Override
public void Run () {//run () is equivalent to main function (), except that we are only in the run () that is open in main
Synchronized (this) {//Use a synchronous block of code to ensure that only one thread at a time accesses the code inside the code block, guaranteeing the security of the data, this means that different threads use the same monitor (Mythread object)
for (int i=0;i<5;i++) {
try {
j + +;
System.out.println (Thread.CurrentThread (). GetName () + "value of J" +j);
Thread.Sleep (1000);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
}
}
The result of the output is:
The value of thread-1j 1
The value of thread-1j 2
The value of thread-1j 3
The value of thread-1j 4
The value of thread-1j 5
The value of THREAD-0J 6
The value of THREAD-0J 7
The value of THREAD-0J 8
The value of THREAD-0J 9
The value of THREAD-0J 10
Summarize:
Create a Thread object (New thread (Runnable Runnable)) and start (Start ()) This thread is actually creating a thread and then executing the thread target (Runnable)
The run () method, there are several threads (note that a thread object can only be opened once, this refers to having several thread objects and start) executing several rum () methods, The Rum () method
equals the main () method in the main function (if main () is not dormant, then the number of start times plus 1 threads are running simultaneously). The properties of Runnable and run () are the same level,
Therefore, the properties and methods of the thread target (Runnable) can be shared by the run (), that is, it can be called and shared by different threads. In order to ensure that properties and methods are shared by threads, we must make the threads use properties and methods in an orderly manner.
This leads to the concept of thread synchronization, using
Synchronized (this) {}//Synchronous code block, this can be any object, but this object must be a common monitor for all threads
The synchronized//method modifier, whose monitor is the object of the current thread class, behaves like this
These two methods ensure that a block of code or a function is fully executed before being used by the next thread.
---restore content ends---
Learning notes for Java threads