In addition, compared with thread, in addition to inheritance, how is the independence of code and data reflected in runnable? As written on some blogs, thread cannot share resources, but runnable can share resources. Isn't it enough to change the variables in thread to static? Just like the following article http://blog.csdn.net/uudou/ar...
It doesn’t seem to have much to do with data. I think Runnable has two benefits:
After implementing Runnable, you can open a thread to run (usually use
executorService.exec(command)
, you can also usenew Thread(command).start()
if you are a little frustrated), or you can not open the thread blocking method Run (directly callcommand.run()
);Java 1.8 and later can be run with Lambda, for example:
The advantage of
Runnable
is that it can be used in various scenarios. For example, you can make anyClass implements Runnable
, butextends Thread
has some limitations. Because of Java single inheritance, it cannot be used in some scenarios. .Answer:
This problem is considered a design problem.
The reason why Thread and Runnable are separated is to completely separate the "creation process" of the thread from the "execution logic" of the thread.
In other words:
The thread creation process is "code";
The execution logic of the thread is "data";
This sounds a bit confusing, isn’t it all JAVA code? Why does code become data again?
We are not entangled in these concepts. I think we can think about this issue in reverse and give an example to illustrate the problem.
Discussion process:
For example, I want to design a single-threaded program. This single-thread needs to complete two tasks:
1. Print a sentence hello world;
2. Calculate the sum of the two numbers int a and int b and output it;
Note: What is execution 1? Or 2? It is determined by the parameter n, n is a random number...
In order to execute these two tasks in the same thread, we can write code like this:
The above code can indeed complete the task, but the problem is that we confuse the "creation process" and "business logic" of the thread...
This is not good. By the way, from an operating system level, the thread creation process is actually very complicated!
The Java language encapsulates this complexity out of sight. Although the code is just a Thread class and there seems to be no threshold for calling it, the creation process of Thread is still very complicated and consumes resources.
Back to business, now I add a small requirement again. In addition to the previous 1 and 2, I also add a 3 to display the current timestamp of the system.
So the task becomes:
1. Print a sentence hello world;
2. Calculate the sum of the two numbers int a and int b and output it;
3. Display the current timestamp of the system;
Note that at this time we need to modify the creation process of Thread, that is, modify the start function:
This discussion ends, let us observe carefully...Actually:
This part of the code remains unchanged, only the code in the start function is modified as the needs change.
So can we package this part of the changed content into an interface? ?
This should be a good idea!
I don’t know if you fully understand it by now? :D
Haha, doesn’t Java’s Thread class just provide a constructor with Runnable parameters?
We put the business code into the implementation class of the Runnable interface:
So finally, we can call it like this:
This completes the complete separation of the "creation process" and "business logic" of the thread! This "split" also paved the way for Java Thread Pool technology.
To be honest, Thread t = new Thread() { ... } in the sample code is simple enough, but creating a Thread in the thread pool is not that simple.
So "split" is very necessary!
Also, can we imagine:
What happens if the Runable implementation class contains a Runnable list?
Summary:
1. The purpose of using the Runnable interface is to completely separate the "creation process" of the thread from the "execution logic" of the thread;
2. Thread cannot share resources, but Runnable can share resources. This statement is incorrect;
3. In During the discussion, we went from concrete to abstract;
4. The code I gave in the example is indeed relatively simple, but I hope it can explain the problem;
Okay, the above is my answer to this question, I hope it will be helpful to you.