-
1. Design a thread, generate three thread objects, and set the sleep time of the three threads, 10s, 20s, and 30s. It mainly uses thread class and runable interface.
2. The thread class is defined in the package, as long as a class inherits the thread class, this class is a multi-threaded operation class implemented using the threa class.
3. Test the results of the operation.
4. Using the runnable interface, there is no thread name field in the class, so a name attribute is established to save the thread name.
5. Use the runnable interface, new thread().start().
-
1.Inherit the thread class and override the run method.
Threads that inherit the thread class are implemented as follows:
2.By implementing the runnable interface, rewrite the run method 3Create threads through callable and futuretask (as shown in the figure above, the futuretask class implements the runnablefuture interface, and the runnablefuture interface inherits the runnable interface and the future interface, at this time, it can be seen that it is finally called through the runnable interface. future helps to obtain the return value during the execution of the callable interface or terminate the execution of the callable interface).
There are three common methods for the future interface.
A: Create an implementation class of the callable interface and implement the call method B: Create an implementation of the callable implementation class, wrapping the callable object with the FutureTask class, which encapsulates the return value of the call method of the callable object.
C: Create and start a thread using the FutureTask object as the thread object.
d: Call get() of the FutureTask object to get the return value of the end of the execution of the child thread.
Print Result: 3Threads are created by using thread pools.
Comparison A comparison of the implementation interface and the inheritance thread class.
Comparison of runnable and callable interfaces.
-
Answer]: cc. Analysis: This question tests the candidate's use of the creation thread.
There are two ways to create a thread: by implementing the runnable interface and by inheriting the thread class. When an object of a class that implements the runnable interface is used to create a thread, the start of the thread will cause the object's run() method to be called.
To create a thread by inheriting from the thread class, you can define the thread body by inheriting the thread class and overriding the run() method in it, and then create the object of that subclass to create a thread. Thread creation is the key content of the Rock Source exam, so be sure to master it.
1) A controlling merger refers to the acquisition of control over the merged party or the acquired party by the merging party or the purchaser in a business combination; >>>More
There are several ways to finance a business: >>>More
Power generation methods include: thermal power generation, hydropower generation, nuclear power generation, wind power generation, geothermal power generation, etc. >>>More
In terms of general direction, it can be divided into two main channels: "online marketing" and "offline marketing". >>>More
In China, the mainstream broadband access modes are as follows: ADSL, VDSL, FTTB+LAN dynamic IP+Web authentication, FTTB+LAN static IP+Web authentication, wired broadband Internet access, etc. Many access schemes use routers, but compared with different access methods, the setting methods of the routers are not the same, today we will take the broadband road with more compatibility TP-Link as an example, and talk in detail about the specific methods of router settings under different broadband mode access. >>>More