One what is a thread
A modern operating system creates a process for a program when it is running. For example, when you start a Java program, the operating system creates a Java process. Thread concept is defined on the basis of process, thread is the smallest unit that modern operating system can dispatch, it is included in the process, it is the actual operation unit of the journey. A thread refers to a single sequential flow of control in a process in which multiple threads can be run and each thread performs a different task in parallel.
A Java program executes from the main () method and then executes according to the established code logic, seemingly without the involvement of other threads, but in reality Java programs are inherently multithreaded, because the execution of the main () method is a name called the Mian () process. A most common Java program with at least one of the following threads:
public class Main {public static void main (string[] args) {Threadgroup group = Thread.CurrentThread (
). Getthreadgroup ();
Threadgroup topgroup = Group;
Traverses the thread group tree to get the root thread group while (group!= null) {topgroup = group;
Group = Group.getparent ();
The number of threads that are activated is added one more times to prevent the enumeration from possibly having just the dynamic thread generating int slacksize = Topgroup.activecount () * 2;
thread[] slackthreads = new Thread[slacksize];
Gets all threads under the root thread group, and the returned ActualSize is the final number of threads int actualsize = Topgroup.enumerate (slackthreads);
thread[] atualthreads = new Thread[actualsize];
Copy valid values in Slackthreads to Atualthreads system.arraycopy (slackthreads, 0, atualthreads, 0, ActualSize);
System.out.println ("Threads size is" + atualthreads.length);
for (Thread thread:atualthreads) {System.out.println (Thread.getname ());
}
} }
Print results:
Threads size is 5
Reference Handler //purge Reference thread
Finalizer //Invoke object Difalize method interface
Signal Dispatcher //distribution handles the thread that sends the JVM signal
main //main thread, user program entry
Two why use multithreading
One reason to use multithreading is that it is a multitasking operation that often costs less, switches fast, and is more "frugal" than the process. Under Linux, starting a new process must be assigned to its own address space, creating numerous data tables to maintain its code snippets, stack segments, and data segments, which is an "expensive" multitasking way of working. While running on multiple threads in a process, they use the same address space, share most of the data, and start a thread much less space than it takes to start a process, and the time it takes to switch between threads is much less than the time it takes to switch between processes.
The second reason to use multithreading is the convenient communication mechanism between threads. For different processes, they have a separate data space, the transmission of data can only be carried out by means of communication, which is not only time-consuming, but also inconvenient. Threads Otherwise, because the data space is shared between threads under the same process, the data for one thread can be used directly for other threads, which is quick and convenient. Of course, the sharing of data also brings some other problems, some variables can not be modified by two threads at the same time, some subroutines declared as static data is more likely to bring a catastrophic attack on multithreaded programs, which is the most need to note when writing multithreaded program.