Basic concepts of java multithreading

1) What is a thread?

Thread is the smallest unit that the operating system can perform operation scheduling. It is included in the process and is the actual operation unit in the process. Programmers can use it for multiprocessor programming, and you can use multithreading to speed up computationally intensive tasks. For example, if it takes 100 milliseconds for one thread to complete a task, it only takes 10 milliseconds to complete the task with ten threads. Java's excellent support for multithreading at the language level is also a great selling point. For more details click here .

2) What is the difference between a thread and a process?

Threads are a subset of processes, and a process can have many threads, each executing a different task in parallel. Different processes use different memory spaces, and all threads share the same memory space. Don't confuse it with stack memory, each thread has a separate stack memory for storing local data. For more details click here .

3) How to implement threads in Java?

At the language level there are two ways. An instance of the java.lang.Thread class is a thread, but it needs to call the java.lang.Runnable interface to execute. Since the thread class itself is the Runnable interface to call, you can inherit the java.lang.Thread class or directly call the Runnable interface to reset Write the run() method to implement the thread. For more details click here .

4) Use Runnable or Thread?

This question is a follow-up to the previous question. Everyone knows that we can implement threads by inheriting the Thread class or calling the Runnable interface. The question is, which method is better? When to use it? This question is easy to answer if you know that Java does not support multiple inheritance of classes, but allows you to call multiple interfaces. So if you want to inherit other classes, of course, call the Runnable interface. For more details click here .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326198894&siteId=291194637
Recommended