[英]Thread termination difference between C & Java
From my understanding, a C process will be terminated as soon as the main (parent) thread returns or reaches the end of execution. 据我了解,一旦主(父)线程返回或到达执行结束,C进程将终止。 (This can be changed using pthread_join in the main thread).
(这可以在主线程中使用pthread_join进行更改)。
However, a Java process will still keep running until all the threads finishes their execution even if the main thread has returned or finished. 但是,即使主线程已返回或已完成,Java进程仍将继续运行,直到所有线程完成其执行。 (This can be changed by making the child threads run as daemons in Java).
(这可以通过使子线程在Java中作为守护程序运行来更改)。
Why did Java choose to do it this way? Java为什么选择采用这种方式?
#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;
}
Thread created successfully
First thread processing
Thread created successfully
Second thread processing
Process Exit
Neither thread finishes it's task before process exits. 在进程退出之前,两个线程都没有完成任务。
However, in 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.");
}
}
Thread 1 starting.
Thread 2 starting.
Thread running.
Main thread finished.
Thread running.
Thread ending.
Thread ending.
Both threads completes its task even though the main thread has finished long back. 即使主线程早已结束,两个线程仍完成其任务。 As can be seen, there are no daemon threads.
可以看出,没有守护程序线程。
Edit: From Java doc : 编辑:从Java doc :
The Java Virtual Machine continues to execute threads until all threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method Java虚拟机将继续执行线程,直到不是守护程序线程的所有线程都死亡为止,方法是从调用返回到run方法,或者引发一个传播到run方法之外的异常。
From my understanding, a C process will be terminated as soon as the main (parent) thread returns or reaches the end of execution.
据我了解,一旦主(父)线程返回或到达执行结束,C进程将终止。
No. AC process will not terminate just because the first thread reaches the end of its execution. 不会。AC进程不会仅仅因为第一个线程到达其执行结束而终止。 To prove it, change your code from:
为了证明这一点,请从以下位置更改代码:
sleep(2);
printf("\nProcess Exit\n");
return 0;
}
To: 至:
sleep(2);
printf("\nThread Exit\n");
pthread_exit(0);
return 0; // will never execute
}
The process will terminate if the first thread returns from main
. 如果第一个线程从
main
返回,则该过程将终止。 The C standard (section 5.1.2.2.3) says that returning from main
is equivalent to calling exit
, and this is what terminates the process. C标准(5.1.2.2.3节)说,从
main
返回等价于调用exit
,这就是终止进程的原因。 The thread then terminates because the process terminates, not the other way around. 然后线程终止,因为进程终止了,而不是相反。
Your statement is incorrect in several aspects: 您的陈述在几个方面是不正确的:
The Java application will exit upon the main thread terminates, unless you have any daemon threads. 除非您有任何守护程序线程,否则Java应用程序将在主线程终止时退出。
And the C++ program won't immediately terminate - after the main thread finishes, there are still clean up to do before the entire process is finished. 而且C ++程序不会立即终止-主线程完成后,在整个过程完成之前仍需要清理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.