Thread summary

xiaoxiao2021-03-06  104

There are two ways to implement the thread, inherit the Thread class or implement the runnable interface, both of which can achieve multi-threaded functions well.

For runnable interface, as long as you want to implement multithreadable code in the RUN method, then create a THREAD instance in any place where you want to run threads to run, the Runnable interface class is implemented as a stream of THREAD object. In, and call the START method to start the thread.

For inherited Thread classes, as long as the thread code is written in the RUN method, and the instance of the thread class directly generates the thread class in any other class, and call the START method to start the thread.

the difference:

1. RunNable is suitable for extension existing classes, making it multi-threaded feature, inheriting Thread is suitable for encapsulating a separate thread function. Runnable is easy to access members to extend class.

2. Runnable just implements the functionality to do, this class is not considered threads, which must be incomplated in a thread class to become a thread, and the thread class controls the state of the thread. Inheriting the Thread class can achieve thread function and freely controlled by its own subclass.

Common:

1. No matter which method implemented thread, you can call the SLEEP method in your code, just writing a different way. Sleep is a static method of the Thread class, and the Runnable interface itself is no such method. Therefore, it is necessary to call with Thread.Sleep in runnable, the purpose is to perform the thread sleep of the current code, which is equivalent to thread.currentthread (). Sleep (), and for the subclass of THREAD, call the Sleep method naturally That is to say, the method of controlling the Runnable interface or Thread subclass is consistent.

2. The code in the run includes a function call to function code. In the execution process, it is a part of the thread code. The exception here is a function code that is automatically called by the system, such as: repaint () Paint () called.

Synchronize:

Synchronized keyword can appear in any code that might want to do thread synchronization processing, not necessarily in the Thread subclass or in the runnable interface or in RUN, it can be used for any class, and this class may be threaded Call, therefore doing thread synchronization processing. At the same time, SYNCHRONIZED can also be used in any synchronous code that requires synchronous processing, and this location is not subject to any restrictions.

Communication:

Wati, Notify, NotifyAll is a static method of the Object class. All classes will inherit the three methods, which can only be used in code marked with synchronized, which is notified to call the code to complete a certain action.

example:

Import java.applet. *; import java.awt. *;

Public class myapplet experts applet imports = 0; thread t; public void init (); t.start ();}

Public void Paint (GRAPHICS G) {Count ; g.drawstring (" count, 10, 50);} public void run () {for (int i = 0; i <20; i ) {repaint (); try {Thread.sleep (500);} catch (exception e) {E.PrintStackTrace ();}}}} The above example is the above example, and the number of 0 to 19 in the window is implemented. If the thread is not implemented, the number is It cannot be displayed in the form because the loop operation occupies a large amount of CPU, and the thread of the form re-draw is less than the time to redraw the form. Therefore, to complete such a function must be used to thread, and the method of calling redrawing is placed in the RUN method, realizing multi-thread, and then, each call will call the current thread code sleep 0.5 seconds, Thread .sleep (500). Generate a thread subclass in the init method, for manipulating the thread, incorporating the objects of the current class into this thread object when the thread object is instantiated, because the current class implements the RUN method, that is, the work to be completed.

转载请注明原文地址:https://www.9cbs.com/read-124442.html

New Post(0)