There are two ways to create threads in Java: Using the thread class and using the Runnable interface. A thread instance needs to be established when using the Runnable interface. Therefore, no matter
Whether a thread is established through the thread class or the Runnable interface, you must establish an instance of the thread class or its subclasses
The main thread may also end before the child thread ends. And the child threads are not affected, and will not end because of the end of the main thread.
In Java, at least 2 threads are started each time a program runs. One is the main thread and one is the garbage collection thread. Because every time a class is executed with a Java command, the actual
A JVM is started on the other, and each JVM internship is a process initiated in the operating system.
The main method is actually a thread. In Java so the threads are all started at the same time, as to when, which first executes, fully see who first get the CPU resources.
The benefits of implementing the Runnable interface are more than inheriting the thread class:
such as ticketing system, votes count, implement runnable way to start multiple threads, multiple threads handle the same count (one thread minus one after the other threads are processed is minus one after
Thread, and each thread has a count that does not affect each other (after one thread has been reduced, the other threads are not processing the thread minus one.
data, but other threads ' own data)
1): Suitable for multiple threads of the same program code to process the same resource
2): Can avoid the restriction of single inheritance in Java
3): Increase the robustness of the program, the code can be shared by multiple threads, code and data Independent
The so-called synchronization is that only one thread runs in the unified time period, and the other threads must wait until the thread finishes before continuing.
With synchronization, you can use synchronous code blocks and synchronization methods to do both.
"Synchronizing code blocks":
Syntax format:
Synchronized (synchronization object) {
Code that needs to be synchronized
}
However, the current object this is generally used as the synchronization object.
"Synchronization Method"
You can also use synchronous methods.
The syntax format for the Synchronized method returns the type method name (parameter list) {
Other code
}
3. Thread Merge--join
The meaning of a thread merge is to merge threads of several parallel threads into one single-threaded execution, where the thread class provides a join method to accomplish this function when one of the threads must wait for the other thread to execute, noting that it is not a static method.
As you can see from the list of methods above, it has 3 overloaded methods:
void Join ()
The current thread waits for that thread to terminate after it joins the thread.
void join (Long Millis)
The current thread waits for the thread to terminate for a maximum of millis milliseconds. If the thread is not finished during the Millis time, then when the front-end goes into a ready state, it waits for the CPU to dispatch again
void join (Long millis,int Nanos)
The maximum time to wait for the thread to terminate is Millis milliseconds + Nanos nanoseconds. If the thread is not finished during the Millis time, then when the front-end goes into a ready state, it waits for the CPU to dispatch again
Sleep is a static method, who fell who went to sleep, even in the main line thread called the Sleep method of thread B, actually still main go to sleep, want to let thread B go to sleep in the code of B drop sleep
- Public class Test1 {
- public static void Main (string[] args) throws interruptedexception {
- System.out.println (Thread.CurrentThread (). GetName ());
- MyThread mythread=New MyThread ();
- Mythread.start ();
- Mythread.sleep (1000); //Sleep Here is the main thread, not the mythread thread
- Thread.Sleep (10);
- For (int i=0;i<100;i++) {
- SYSTEM.OUT.PRINTLN ("main" +i);
- }
- }
- }
Multi-threaded Learning Notes