日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux非守护线程一直不释放,Linux pthread 和 java thread 的是 / 非守护线程的行为

發(fā)布時(shí)間:2023/12/19 linux 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux非守护线程一直不释放,Linux pthread 和 java thread 的是 / 非守护线程的行为 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Linux pthread 和 java thread 的是 / 非守護(hù)線程的行為

pthread_xxx 的函數(shù)并沒有直接提供設(shè)置一個(gè) pthread 為守護(hù)線程的 API

而 pthread_attr_init() 和 struct pthread_attr_t 也并沒有提供 線程是否被設(shè)置為守護(hù)線程的成員變量

但 java 的線程對象有 setDaemon() 方法將線程設(shè)置為守護(hù)線程

那我們看看 java 的 Thread 的 native 層是如何實(shí)現(xiàn)該變量的功能的voidThread::CreateNativeThread(JNIEnv*env,jobject java_peer,size_tstack_size,boolis_daemon){

...

623Thread*child_thread=newThread(is_daemon);// is_daemon 是關(guān)鍵

...

好像和 native 的 pthread 并沒有什么關(guān)系... 可能和 jvm 里有相關(guān)處理邏輯.

所以說 Linux 下是不存在原生的 pthread 是否為守護(hù)進(jìn)程概念的 (最多只有守護(hù)進(jìn)程概念)

Linux 下 pthread 行為:

L1. 子線程為守護(hù)線程 (默認(rèn)情況, Linux 下創(chuàng)建 pthread 天生就是守護(hù)線程, 行為和 J2. java 中調(diào)用 setDaemon(true) 后一致):

1.mainThread 跑完 main 函數(shù), subThread 立刻退出, mainThread 退出, 整個(gè)進(jìn)程退出

2.subThread 跑完, subThread 退出, mainThread 跑完 main 函數(shù), mainThread 退出, 整個(gè)進(jìn)程退出

java 下 thread 行為:

J1. 子線程不為守護(hù)線程 (默認(rèn)情況, java 下創(chuàng)建 thread 天生不是守護(hù)進(jìn)程):

1.mainThread 跑完 main 函數(shù), subThread 立刻退出, mainThread 退出, jvm 退出, 進(jìn)程退出

2.subThread 跑完, subThread 退出, mainThread 跑完 main 函數(shù), mainThread 退出, jvm 退出, 進(jìn)程退出

J2. 子線程為守護(hù)線程 (調(diào)用 setDaemon(true), 和 Linux 下子線程創(chuàng)建后, 行為和 Linux 中默認(rèn)創(chuàng)建 pthread 后一致):

1.mainThread 跑完 main 函數(shù), subThread 繼續(xù)跑到退出, subThread 退出, mainThread 退出, jvm 退出, 進(jìn)程退出

2.subThread 跑完, subThread 退出, mainThread 跑完 main 函數(shù), mainThread 退出, jvm 退出, 進(jìn)程退出

總結(jié)

1. 守護(hù)線程行為

Linux 下默認(rèn)創(chuàng)建線程 = java 下創(chuàng)建后調(diào)用 setDeamon(true)

行為: 只要主線程執(zhí)行完了, 守護(hù)線程立刻銷毀, 進(jìn)程退出

2. 非守護(hù)線程行為

Linux 下無法直接將線程設(shè)置為非守護(hù)進(jìn)程 = java 下默認(rèn)創(chuàng)建的線程默認(rèn)就是非守護(hù)線程

行為: 任何一個(gè)非守護(hù)線程執(zhí)行完了, 立刻銷毀自己 (無論主線程還是子線程), 整個(gè)進(jìn)程中非守護(hù)進(jìn)程數(shù)量 > 0, 進(jìn)程不會(huì)退出; 整個(gè)進(jìn)程中非守護(hù)進(jìn)程數(shù)量 == 0, 立刻剩余銷毀守護(hù)進(jìn)程, 并退出進(jìn)程

L1 代碼:

L1.1.#include

#include

#include

voidmsleep(unsignedints){

printf("tid: %d, start sleep %d\n",pthread_self(),s);

sleep(s);

printf("tid: %d, end sleep %d\n",pthread_self(),s);

}

intfun2(){

printf("fun2() start\n");

pthread_detach(pthread_self());

printf("current tid:%d\n",pthread_self());

msleep(5);

printf("fun2() end\n");

return1;

}

voidmain(){

intres;

pthread_tpt2;

printf("main() start\n");

//pthread_attr_init();

res=pthread_create(&pt2,NULL,fun2,NULL);

//printf("res: %d\n",res);

msleep(3);

printf("main() end\n");

}

輸出[emailprotected]:~/cdir/dthreaddemo# ./dt1

main()start

tid:-1624217856,start sleep3

fun2()start

current tid:-1632536832

tid:-1632536832,start sleep5

tid:-1624217856,endsleep3

main()end

可以看到并沒有 fun2() end 輸出

L2.2.

代碼#include

#include

#include

voidmsleep(unsignedints){

printf("tid: %d, start sleep %d\n",pthread_self(),s);

sleep(s);

printf("tid: %d, end sleep %d\n",pthread_self(),s);

}

intfun2(){

printf("fun2() start\n");

pthread_detach(pthread_self());

printf("current tid:%d\n",pthread_self());

msleep(3);

printf("fun2() end\n");

return1;

}

voidmain(){

intres;

pthread_tpt2;

printf("main() start\n");

//pthread_attr_init();

res=pthread_create(&pt2,NULL,fun2,NULL);

//printf("res: %d\n",res);

msleep(5);

printf("main() end\n");

}

輸出[emailprotected]:~/cdir/dthreaddemo# ./dt1

main()start

tid:-1453377792,start sleep5

fun2()start

current tid:-1461696768

tid:-1461696768,start sleep3

tid:-1461696768,endsleep3

fun2()end

tid:-1453377792,endsleep5

main()end

J1.1

代碼publicclassTest1{

publicstaticvoidmain(String[]args){

System.out.println("main start");

Threadt2=newThread(newRunnable(){

@Override

publicvoidrun(){

System.out.println("t2 start");

try{

Thread.sleep(5000);

}catch(InterruptedExceptione){

e.printStackTrace();

}

System.out.println("t2 end");

}

});

// t2.isDaemon(); //default is false

t2.setDaemon(false);

t2.start();

try{

Thread.sleep(3000);

}catch(InterruptedExceptione){

e.printStackTrace();

}

System.out.println("t2.isAlive()"+t2.isAlive());

System.out.println("main end");

}

}

輸出main start

t2 start

t2.isAlive()true

mainend

t2end

Processfinishedwithexitcode0

J1.2.

代碼publicclassTest1{

publicstaticvoidmain(String[]args){

System.out.println("main start");

Threadt2=newThread(newRunnable(){

@Override

publicvoidrun(){

System.out.println("t2 start");

try{

Thread.sleep(3000);

}catch(InterruptedExceptione){

e.printStackTrace();

}

System.out.println("t2 end");

}

});

// t2.isDaemon(); //default is false

t2.setDaemon(false);

t2.start();

try{

Thread.sleep(5000);

}catch(InterruptedExceptione){

e.printStackTrace();

}

System.out.println("t2.isAlive()"+t2.isAlive());

System.out.println("main end");

}

}

輸出main start

t2 start

t2end

t2.isAlive()false

mainend

Processfinishedwithexitcode0

J2.1.

代碼publicclassTest1{

publicstaticvoidmain(String[]args){

System.out.println("main start");

Threadt2=newThread(newRunnable(){

@Override

publicvoidrun(){

System.out.println("t2 start");

try{

Thread.sleep(5000);

}catch(InterruptedExceptione){

e.printStackTrace();

}

System.out.println("t2 end");

}

});

// t2.isDaemon(); //default is false

t2.setDaemon(true);

t2.start();

try{

Thread.sleep(3000);

}catch(InterruptedExceptione){

e.printStackTrace();

}

System.out.println("t2.isAlive()"+t2.isAlive());

System.out.println("main end");

}

}

輸出main start

t2 start

t2.isAlive()true

mainend

Processfinishedwithexitcode0

J2.2.

代碼publicclassTest1{

publicstaticvoidmain(String[]args){

System.out.println("main start");

Threadt2=newThread(newRunnable(){

@Override

publicvoidrun(){

System.out.println("t2 start");

try{

Thread.sleep(3000);

}catch(InterruptedExceptione){

e.printStackTrace();

}

System.out.println("t2 end");

}

});

// t2.isDaemon(); //default is false

t2.setDaemon(true);

t2.start();

try{

Thread.sleep(5000);

}catch(InterruptedExceptione){

e.printStackTrace();

}

System.out.println("t2.isAlive()"+t2.isAlive());

System.out.println("main end");

}

}

輸出main start

t2 start

t2end

t2.isAlive()false

mainend

Processfinishedwithexitcode0

這里不考慮線程調(diào)度的極端問題, 如活鎖之類的, 或?qū)е履硞€(gè)線程一致無法被分配到時(shí)間片等情況.

來源: http://www.bubuko.com/infodetail-3388958.html

總結(jié)

以上是生活随笔為你收集整理的linux非守护线程一直不释放,Linux pthread 和 java thread 的是 / 非守护线程的行为的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。