Java Concurrent Programming: thread and process creation (conversion)

Java Concurrent Programming: how to create threads?

In the previous article, we have described the origin of processes and threads. Today, let's talk about how to create threads in Java and let threads execute a subtask. Let's talk about the conceptual knowledge related to applications and processes in Java, and then explain how to create threads and processes. The following is the table of contents for this article:

I Concepts related to applications and processes in Java

II How to create threads in Java

III How to create a process in Java

If there is anything wrong, please understand and welcome criticism and correction.

Please respect the author's labor achievements. Please indicate the original link for Reprint:

   http://www.cnblogs.com/dolphin0520/p/3913517.html

I Concepts related to applications and processes in Java

In Java, An application corresponds to a JVM instance (also called JVM process in some places). Generally speaking, the default name is java.exe or javaw.exe (you can view it through the task manager under Windows). Java adopts the single thread programming model, that is, if we do not actively create a thread in our own program, only one thread will be created, which is usually called the main thread. However, it should be noted that although there is only one thread to execute tasks, it does not mean that there is only one thread in the JVM. The JVM instance will be created at the same time Create many other threads (such as garbage collector threads).

Because Java adopts the single thread programming model, it is necessary to pay attention to the time-consuming operations in the sub threads during UI programming to avoid blocking the main thread (in UI programming, the main process, i.e. UI thread, is used to handle user interaction events).

II How to create threads in Java

In Java, there are two ways to create threads: 1) inherit the thread class; 2) Implement the runnable interface.

  1. Inherit thread class

If you inherit the thread class, you must override the run method and define the tasks to be executed in the run method.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>