Java Concurrency Programming Practical manual (i) thread management

Source: Internet
Author: User

This article is mainly in the form of knowledge points to Java multithreading, Learning Java Multi-Threading Foundation, this article refers to the book "Java Concurrency Programming Practical manual", if you are interested in studying and advanced multithreading ideas, you can read "Java Concurrent programming Practical".  

1. Creation and operation of threads

There are three ways to create Java threads, and most people may only know two common types:

1. Inherit the thread class and overwrite the run () method.

2. Create a class that implements the Runnable interface. Use the thread constructor with parameters to create the thread object.

3. use callable and future to create a startup thread

1. Create the callable implementation class and override the call () method, which is the thread execution body, and the method has a return value of 2. Creates an instance of callable and wraps the callable object with the Futuertask class. The Futuertask encapsulates the return value of the call () method of the Callable object 3. Instantiate the Futuertask class, and the parameter is an object of the Futuertask interface implementation class to start thread 4. The Get () party of the object through the Futuertask class method to get the return value after the end of the thread the third example code is given below:
<span style= "Font-family:comic Sans MS;" >import Java.util.concurrent.callable;import Java.util.concurrent.futuretask;public class ThirdThread implements Callable<integer>{public Integer Call () {int i = 0;for (; i<60; i + +) {System.out.println (Thread.CurrentThread () The value of the. GetName () + "loop variable i" is: "+ i);} return i;}  public static void Main (string[] args) {Thirdthread RT = new Thirdthread ();//Create callable object futuretask<integer> task = New futuretask<integer> (RT);//use Futruetask to wrap callable objects for (int i=0; i<60; i++) {System.out.println ( Thread.CurrentThread (). GetName () + "-------" + i), if (i = =) {New Thread (task, "Thread with return value"). Start ();// The thread}}try{system.out.println ("Return value of the child thread:" + task.get ()) is actually created and started with the callable object; catch (Exception e) {e.printstacktrace ();}}} </span>
2. Threading InformationThe thread class has properties that hold information that can be used to mark threads, show the state of a thread, or control the priority of a thread. ID: A unique identifier for the thread was saved name: the names of the threads are saved
Priority: The precedence of the thread object is saved. The priority of a thread is 1-10, where 1 has the lowest priority. Status: The state of the thread is saved. There are 6 new, runnable, blocked, waiting, time waiting, or terminated states in the Java thread.3. Interrupts for ThreadsIf a Java program has more than one thread of execution, the Java program runs the end when all the threads are running, or, more specifically, when all non-daemons run at the end or when one of the threads calls the System.exit () method, the Java program runs to the end. The Java thread interrupts the Thread.Interrupt () method, which is just an identity that executes this method thread can still continue to run, if you want to respond to this interrupt in the thread can be set, determine whether the interrupt, and return method body, exit execution,   If you want to break multiple methods or break in recursion, a return is not enough, so a throw interruptedexception is required to interrupt execution. Note: It is possible to check the thread's static method interrupted () by checking to see if it is interrupted. This method can change the state of interrupted, and the second, isinterrupted () method is recommended.4. Thread hibernation and waiting thread termination     The first call to the thread's sleep method, and the second is called through the Timeunit enumeration class element. Thrown immediately if a thread is interrupted during hibernationThrow interruptedexception exception. The join method can wait for the thread to end or a certain amount of time to execute the following program. 5. Creation of the daemon threadA daemon thread is a background service thread that, once a user thread is running, is running and can be set to a daemon by Setdaemon. 6. Use of local variables for threadsUsing a Runnable implementation class allows you to create multiple threads, but the variables in this class are shared, in order to avoid introducing threadlocal in this case. Example:
private static threadlocal<date> StartDate = new threadlocal<date> () {  protected Date InitialValue () {    return new Date ();     }};
Threadlocal stores the respective property values for each thread. 7. Thread grouping with the use of the factory create thread when creating threads can be said thread join Threadgroup. Use the factory to create the thread, implement the Threadfactory interface, and override the Newthread method.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Concurrency Programming Practical manual (i) thread management

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.