Topic
Explains the two most common ways to create threads and their comparisons.
Answer
method One: Inherit the Thread class implementation
Steps:
- Create a subclass of the Thread class, such as MyThread.
- Override the run () method of the Thread class.
- Instantiate the MyThread class with object names such as MyThread.
- Use the start () method of the thread class to start a thread, such as Mythread.start ().
method Two: Implement Runnable interface
Steps:
- Create a class, such as Myrunnablethread, to implement the Runnable interface.
- Creates an object of the Myrunnablethread class.
- Instantiate the thread class, object name such as thread, and pass to its constructor the Myrunnablethread class and the thread name two parameters.
- Use the start () method of the thread class to start a thread, such as Thread.Start ().
Inheriting the thread class creating threads differs from implementing the Runnable interface creation thread in that when multiple threads are created with the same class, the former actually creates several different thread objects, and its internal The Run () method executes in its own object, does not interfere with each other, as many threads perform multiple tasks, while the latter actually uses the same object to create multiple threads, so the properties within the object are common, which is equivalent to multiple threads performing the same task.
In addition, the class inheriting the thread class cannot inherit other classes, and the class that implements the Runnable interface can inherit other classes, the former is relatively limited.
This may be a bit abstract, the following code to explain.
Reference Code
MyThread Class
PackageMe.huangzijian; Public classMyThreadextendsThread {Private intnum = 10; PrivateString name; PublicMyThread (String name) { This. Name =name; } @Override Public voidrun () {intCount =num; for(inti = 0; I < count; i++) {System.out.println ( This. Name + ":" +num); Num--; } }}
myrunnable Class
PackageMe.huangzijian; Public classMyrunnablethreadImplementsRunnable {Private intnum = 10; @Override Public voidrun () {intCount =num; for(inti = 0; I < count; i++) {System.out.println (Thread.CurrentThread (). GetName ()+ ":" +num); Num--; } }}
Theadcreation class
PackageMe.huangzijian; Public classthreadcreation { Public Static voidMain (string[] args) {//inheriting the thread class implementationMyThread MyThread1 =NewMyThread ("MyThread1"); MyThread myThread2=NewMyThread ("MyThread2"); MyThread myThread3=NewMyThread ("MyThread3"); Mythread1.start (); Mythread2.start (); Mythread3.start (); //implementing the Runnable interfaceMyrunnablethread Myrunnablethread =NewMyrunnablethread (); Thread T1=NewThread (Myrunnablethread, "MyRunnableThread1"); Thread T2=NewThread (Myrunnablethread, "MyRunnableThread2"); Thread T3=NewThread (Myrunnablethread, "MyRunnableThread3"); T1.start (); T2.start (); T3.start (); }}
Operation Result:
After running threadcreation , we will see that the result of inheriting the Thread class implementation is as follows:
As you can see, the three-thread operation for the field Num is each operation, reducing num from ten to 1.
The implementation of the Runnable interface is as follows:
As you can see, three threads work together on num for the same myrunnablethread object. Depending on the program, each thread loops ten times, so num has been reduced from ten to negative. Some friends may ask, why the first three threads will get to ten, this is the thread synchronization problem, need to use the synchronized and other keywords to decorate.
A Java question every day [10]