1. Definition
Threads are the smallest unit of modern operating system calls, also known as lightweight processes, where multiple threads can be created in one process, each with properties such as counters, stacks, and local variables, and access to shared memory variables.
2. What threads are included in a common Java program
Below is the code output in the JDK1.8.0_60 environment.
1 Importjava.lang.management.ManagementFactory;2 ImportJava.lang.management.ThreadInfo;3 ImportJava.lang.management.ThreadMXBean;4 5 Public classmultithread {6 Public Static voidMain (string[] args) {7 //get Java thread management Mxbean8Threadmxbean Threadmxbean =Managementfactory.getthreadmxbean ();9threadinfo[] Threadinfos = Threadmxbean.dumpallthreads (false,false);Ten //traverse thread information, print only thread ID and thread name information One for(Threadinfo threadinfo:threadinfos) { ASystem.out.println ("[" + threadinfo.getthreadid () + "]" +threadinfo.getthreadname ()); - } -System.out.println (System.getproperty ("Java.version")); the } -}
Output Result:
[5] Attach Listener //is responsible for receiving external commands, execute the command and return the results to the sender [4] Signal Dispatcher //distribution processing the thread sent to the JVM signal [3] Finalizer //Call the thread of the object Finalize method [2] Reference Handler //Clear Reference thread [1] main //main thread, user program entry 1.8.0_60 // JDK Version number
3. Priority level
In a Java thread, priority is controlled by an integer member variable priority, which has a range of 1-10, and can be modified by the setpriority (int) method when the thread is built, and the default priority is 5. Higher priority threads allocate time slices with less priority threads. There are differences in thread planning on different JVMs and operating systems, and some operating systems even ignore thread priority settings.
4. Priority examples
My computer is win7,64,jdk1.8, and the output indicates that the priority is in effect.
1 Packagethread;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 ImportJava.util.concurrent.TimeUnit;6 7 Public classPriority {8 Private Static volatile BooleanNotstart =true;9 Private Static volatile BooleanNotend =true;Ten One Static classJobImplementsrunnable{ A Private intPriority ; - Private LongJobcount; - PublicJob (intPriority ) { the This. Priority =Priority ; - } - @Override - Public voidrun () { + while(Notstart) { - Thread.yield (); + } A while(notend) { at Thread.yield (); -jobcount++; - } - } - } - Public Static voidMain (string[] args)throwsinterruptedexception { inlist<job> jobs =NewArraylist<job>(); - for(inti = 0; I < 10; i++){ to //setting the priority level + intPriority = I < 5?Thread.MIN_PRIORITY:Thread.MAX_PRIORITY; -Job Job =NewJob (priority); the jobs.add (Job); *Thread thread =NewThread (Job, "Thread:" +i); $ thread.setpriority (priority);Panax Notoginseng Thread.Start (); - } theNotstart =false; +TimeUnit.SECONDS.sleep (2); ANotend =false; the for(Job job:jobs) { +System.out.println ("Job Priority:" +job.priority -+ ", Count:" +job.jobcount); $ } $ } -}
Output results
1 job priority:1, Count:6607305 2 Job P Riority:1, Count:3393520 3 Job priority:1, Count:11684488 4 job priority:1, Count:8878447 5 job priority:1, count:660713 0 6 job priority:10, Count:12661281 7 Job Pri Ority:10, Count:12467001 8 Job priority:10, Count:12306292 job priority:10, Count:1242501110 job Priority:10, Count:12 817522
5, the state of the thread
Java threads may be in 6 different states during the life of the run, and one moment can only be one state.
NEW: The initial state, the thread is built, but the start () method has not been called;
RUNNABLE: Running state, Java thread will be in the operating system is ready and running the two States are generally referred to as "running";
BLOCKED: Blocking state, indicating that the thread is blocking the lock;
Waiting: The wait state, which indicates that the thread is in a wait state, enters that state to indicate that the current thread needs to wait for other threads to make certain actions (notifications or interrupts);
Time_wating: Timeout Wait state, which is different from waiting, which can be returned on its own at a specified time;
TERMINATED: The terminating state indicates that the current thread has finished executing.
6. Starting and terminating threads
construct thread : A newly constructed thread object is allocated by its parent thread, and the child thread inherits whether the parent is a daemon (daemon thread), The priority and Contextclassloader of the loaded resource, as well as the inheritable threadlocal, also assign a unique ID to identify the child thread.
start thread : Call the Start () method, which means that the current thread (that is, the parent thread) tells the Java virtual machine synchronously, and the thread that calls the start () method should be started as soon as the County Planner is idle.
expired Suspend (), resume (), Stop (): Not recommended, take the Suspend method as an example, after the call, the thread will not release the resources already occupied (such as locks), but the possession of resources into sleep, which is prone to deadlock problems. Similarly, the Stop method does not guarantee the normal release of thread resources when terminating a thread, usually without giving the thread the opportunity to complete the resource release effort, thus causing the program to potentially work in an indeterminate state.
Java Concurrency Programming (i)