C 和 Java 之间的线程终止差异
Thread termination difference between C & Java
根据我的理解,一旦主(父)线程 returns 或执行结束,C 进程就会终止。 (这可以在主线程中使用 pthread_join 更改)。
但是,Java 进程仍将保持 运行ning 直到所有线程完成执行,即使主线程已返回或完成也是如此。 (这可以通过将子线程 运行 作为 Java 中的守护进程来改变)。
为什么Java选择这样做?
C 代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
pthread_t tid[2];
void *doSomeThing(void *arg) {
unsigned long i = 0;
pthread_t id = pthread_self();
if (pthread_equal(id, tid[0])) {
printf("\n First thread processing\n");
}
else {
printf("\n Second thread processing\n");
}
for (i = 0; i < (0xFFFFFFFF); i++);
printf("\n Threads finished.\n");
return NULL;
}
int main() {
int i = 0;
int err;
while(i < 2)
{
err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
if (err != 0)
printf("\ncan't create thread :[%s]", strerror(err));
else
printf("\n Thread created successfully\n");
i++;
}
sleep(2);
printf("\nProcess Exit\n");
return 0;
}
C输出
Thread created successfully
First thread processing
Thread created successfully
Second thread processing
Process Exit
在进程退出之前,两个线程都没有完成它的任务。
然而,在Java中:
Java 代码示例
public class ThreadTermination implements Runnable {
@Override
public void run() {
System.out.println("Thread running.");
try {
Thread.sleep(5 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread ending.");
}
public static void main(String[] args) {
ThreadTermination t1 = new ThreadTermination();
ThreadTermination t2 = new ThreadTermination();
Thread thr1 = new Thread(t1);
System.out.println("Thread 1 starting.");
thr1.start();
Thread thr2 = new Thread(t2);
System.out.println("Thread 2 starting.");
thr2.start();
System.out.println("Main thread finished.");
}
}
Java 输出
Thread 1 starting.
Thread 2 starting.
Thread running.
Main thread finished.
Thread running.
Thread ending.
Thread ending.
两个线程都完成了它的任务,即使主线程早就完成了。可以看出,没有守护线程。
编辑:来自Java doc:
Java 虚拟机继续执行线程,直到所有不是守护线程的线程都已死亡,方法是从对 运行 方法的调用返回,或者抛出传播到外部的异常运行 方法
您的说法有几个方面不正确:
Java 应用程序将在主线程终止时退出,除非您有任何守护线程。
而且C++程序不会立即终止——主线程结束后,在整个进程结束之前还有清理工作要做。
From my understanding, a C process will be terminated as soon as the main (parent) thread returns or reaches the end of execution.
没有。 C 进程不会仅仅因为第一个线程执行结束而终止。为了证明这一点,请更改您的代码:
sleep(2);
printf("\nProcess Exit\n");
return 0;
}
收件人:
sleep(2);
printf("\nThread Exit\n");
pthread_exit(0);
return 0; // will never execute
}
如果 main
中的第一个线程 returns,进程将终止。 C 标准(第 5.1.2.2.3 节)说从 main
返回等同于调用 exit
,这就是终止进程的原因。然后线程终止,因为进程终止,而不是相反。
根据我的理解,一旦主(父)线程 returns 或执行结束,C 进程就会终止。 (这可以在主线程中使用 pthread_join 更改)。
但是,Java 进程仍将保持 运行ning 直到所有线程完成执行,即使主线程已返回或完成也是如此。 (这可以通过将子线程 运行 作为 Java 中的守护进程来改变)。
为什么Java选择这样做?
C 代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
pthread_t tid[2];
void *doSomeThing(void *arg) {
unsigned long i = 0;
pthread_t id = pthread_self();
if (pthread_equal(id, tid[0])) {
printf("\n First thread processing\n");
}
else {
printf("\n Second thread processing\n");
}
for (i = 0; i < (0xFFFFFFFF); i++);
printf("\n Threads finished.\n");
return NULL;
}
int main() {
int i = 0;
int err;
while(i < 2)
{
err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
if (err != 0)
printf("\ncan't create thread :[%s]", strerror(err));
else
printf("\n Thread created successfully\n");
i++;
}
sleep(2);
printf("\nProcess Exit\n");
return 0;
}
C输出
Thread created successfully
First thread processing
Thread created successfully
Second thread processing
Process Exit
在进程退出之前,两个线程都没有完成它的任务。
然而,在Java中:
Java 代码示例
public class ThreadTermination implements Runnable {
@Override
public void run() {
System.out.println("Thread running.");
try {
Thread.sleep(5 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread ending.");
}
public static void main(String[] args) {
ThreadTermination t1 = new ThreadTermination();
ThreadTermination t2 = new ThreadTermination();
Thread thr1 = new Thread(t1);
System.out.println("Thread 1 starting.");
thr1.start();
Thread thr2 = new Thread(t2);
System.out.println("Thread 2 starting.");
thr2.start();
System.out.println("Main thread finished.");
}
}
Java 输出
Thread 1 starting.
Thread 2 starting.
Thread running.
Main thread finished.
Thread running.
Thread ending.
Thread ending.
两个线程都完成了它的任务,即使主线程早就完成了。可以看出,没有守护线程。
编辑:来自Java doc:
Java 虚拟机继续执行线程,直到所有不是守护线程的线程都已死亡,方法是从对 运行 方法的调用返回,或者抛出传播到外部的异常运行 方法
您的说法有几个方面不正确:
Java 应用程序将在主线程终止时退出,除非您有任何守护线程。
而且C++程序不会立即终止——主线程结束后,在整个进程结束之前还有清理工作要做。
From my understanding, a C process will be terminated as soon as the main (parent) thread returns or reaches the end of execution.
没有。 C 进程不会仅仅因为第一个线程执行结束而终止。为了证明这一点,请更改您的代码:
sleep(2);
printf("\nProcess Exit\n");
return 0;
}
收件人:
sleep(2);
printf("\nThread Exit\n");
pthread_exit(0);
return 0; // will never execute
}
如果 main
中的第一个线程 returns,进程将终止。 C 标准(第 5.1.2.2.3 节)说从 main
返回等同于调用 exit
,这就是终止进程的原因。然后线程终止,因为进程终止,而不是相反。