Java multithreaded programming — multithreading

If you want to implement the definition of multithreading in Java, you need to have a special thread body class to define the execution task of threads, and the definition of this body class is required. It can be completed only by implementing a specific interface or inheriting a specific parent class.

1. Inherit thread class to realize multithreading

Java provides a Java Lang. thread program class. If a class inherits this class, it means that this class is the main class of the thread. However, it does not mean that this class can directly implement multithreading, because it also needs to override a run() method (public void run()) provided in the thread class, which belongs to the main method of the thread.

Example: multithreaded body class

The functions to be performed by multithreading should be defined in the run () method. It should be noted that under normal circumstances, if you want to use a method in a class, you must generate an instantiated object and then call the method provided in the class, but the run () method cannot be called directly because it involves the resource scheduling problem of an operating system, Therefore, to start multithreading, you must use the start () method (public void start()).

Example: multithreaded startup

Through the call at this time, it can be found that although the start () method is called, the final execution is the run () method, and all thread objects are executed alternately.

doubt? Why should the start () method in the thread class be used instead of the run () method for multithreading startup? If you want to understand this problem, the best way is to look at the implementation of the start () method, which can be observed directly from the source code.

It is found that an "illegalthreadstateexception" exception class object will be thrown in the start () method, but the whole program does not use throws or explicit try... Catch to handle it. Therefore, this exception must be a subclass of runtimeException. Each thread class object is only allowed to be started once. If it is started repeatedly, this exception will be thrown. For example, the following code will throw an exception.

In the process of Java program execution, considering the needs of developers at different levels, it supports local operating system function calls, This technology is called JNI (Java Native inteface) technology, but it is not recommended to use it in the java development process. Using this technology, some underlying functions provided by the operating system can be used for some special processing, and the start0 () provided in the thread class means that this method needs to be implemented by different operating systems.

2. Implement multithreading based on runnable interface

Although the definition of multithreading can be realized through the inheritance of thread class, there is always the limitation of single inheritance in Java programs. Therefore, there is a second form of multithreaded main body definition structure in Java: implementing Java Lang. runnable interface, which is defined as follows:

Example: implement the multi-threaded body class through runnable

However, since the thread parent class is not inherited at this time, the inherited method start () is no longer supported in the myThread class at this time. However, if thread. Is not used The start () method cannot start multiple threads. At this time, you need to observe the construction method provided by the thread class:

·Construction method: public thread (runnable target);

· construction method: public thread (runnable target, string name);

Example: start multithreading

In the multithreading implementation at this time, it can be found that since only the runnable interface object is implemented, there is no single inheritance limitation on the thread body class at this time, so this design is a standard design.

In the future development, for the implementation of multithreading, the priority is the implementation of runnable interface, and multithreading is always started through thread class object. This is also in line with the design idea of Java.

3. Relationship between thread and runnable

After a series of analysis, it can be found that there have been two methods in the implementation of multithreading: thread class and runnable interface. If the code structure itself is concerned, it is certainly the most convenient to use runnable, because it can avoid the limitation of single inheritance and better expand functions at the same time.

However, it is also necessary to observe the relationship between thread and runnable structurally, and open the thread class definition:

It is found that the thread class is also a subclass of the runnable interface. When inheriting the thread class, it actually overwrites the run() method of the runnable interface. At this time, let's observe the class structure of the program:

In the multi-threaded design, the structure of agent design pattern is used. The user-defined thread body is only responsible for the implementation of the core functions of the project, and all auxiliary implementations are handled by the thread class.

When the thread starts multithreading, the start () method is called, and then the run () method is found. When a runnable interface object is passed through the construction method of the thread class, the interface object will be saved by the target attribute in the thread class. When the start () method is executed, the run () method in the thread class will be called back, The run () method calls the run () method overridden by the runnable interface subclass.

The essence of multithreading development is that multiple threads can preempt the same resource. Thread mainly describes threads, and the description of resources is completed through runnable.

Example: use ticket selling program to realize concurrent access to resources of multiple threads

4. Callable implements multithreading

In terms of the most traditional development, if you want to implement multithreading, you must rely on runnable, but the runnable interface has a disadvantage: you can't get a return value after the thread is executed, so you can use jdk1 After 5, we propose a new thread implementation interface: Java util. concurrent. Callable interface, first observe the definition of this interface:

It can be found that when callable is defined, a generic type can be set. The type of this generic type is the type of returned data. This has the advantage of avoiding the security risks caused by downward transformation.

The callable interface is similar to runnable. Both are designed for classes whose instances may be executed by another thread.

Example: multithreading using callable

5. Thread running status

For multi-threaded development, the process of writing programs always follows: define the thread body class, and then start the thread through the thread class, but it does not mean that you call the start () method, and the thread has already started running, because the overall thread processing has its own set of running states.

1. Xu Aina of any thread should be encapsulated with thread class, so the thread is started with start (), but actually several threads will enter a ready state when starting, and they are not executed now.

2. After entering the ready state, you need to wait for resource scheduling. When a thread is successfully scheduled, it enters the running state (run() method), but all threads cannot continue to execute. In the middle, you need to generate some suspended states. For example, after a thread executes for a period of time, you need to give up resources, Then the thread will enter the blocking state, and then return to the ready state again;

3. When the run () method is executed, in fact, the main task of the thread ends, and you can directly enter the stop state at this time;

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>