Before the Java language generation, the traditional programming language program at the same time can only single task operation, the efficiency is very low, such as the program is often received data input blocking, only wait until the program to get data can continue to run. With the rapid development of the Internet, this situation is increasingly unbearable: if the network receives data blocking, the background program is in the waiting state and does not continue any operations, and this blocking is often encountered, at this time the CPU resources are white idle up. If you can handle multiple tasks at the same time in the background program, how good! The Java language, which is native to Internet technology, solves this problem, and multithreaded programming is a very important feature of the Java language. In a Java program, we can run multiple, relatively independent threads at the same time, for example, if we create a thread to do data input and output, and create another one to do other data processing in the background, if the input output thread blocks while receiving data, the thread that processes the data is still running. Multithreaded programming greatly improves program execution efficiency and processing ability.
Creation of Threads
We know Java is an object-oriented programming language, Java programming is the design and use of classes, Java provides us with thread class thread to create the thread, the creation of the thread and the creation of ordinary classes of the object of the operation is the same, and the thread is the threads class or its subclasses of the instance object. The following is a statement that creates a thread to start:
Thread thread1=new thread (); file://declares an object instance, that is, creating a thread;
Thread1.run (); FILE://starts the thread with the run () method in the thread class;
From this example, we can create a thread by using the thread () construct method and start the thread. In fact, the start thread, which is the start thread's run () method, and the run () method in the thread class does not have any action statements, so this thread has no action. For a thread to implement a predetermined function, you must define your own run () method. There are usually two ways to define the run () method in Java:
Override the Run () method in the subclass by defining a subclass of the thread class. The instance object of the thread subclass is a thread, which obviously has the thread-body run () method that we designed ourselves, and the startup thread starts the overridden run () method in the subclass.
The interface of the run () method is defined in the interface through the Runnable interface. Interfaces are very similar to classes, and are mainly used to implement special functions, such as multiple inheritance functions for complex relationships. Here, we define a class that implements the Runnable () interface, define its own run () method in that class, and then call the thread class's constructor to create a thread with an instance object of that class as a parameter.