欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

python爬蟲取消或終止線程的方法

這篇文章主要介紹python爬蟲取消或終止線程的方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

10余年的興安盟網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。網(wǎng)絡(luò)營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整興安盟建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)公司從事“興安盟網(wǎng)站設(shè)計(jì)”,“興安盟網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

三種線上方法

l 使用退出標(biāo)志,使線程正常退出,也就是當(dāng) run() 方法完成后線程終止;

l 使用 stop() 方法強(qiáng)行終止線程,但是不推薦使用這個(gè)方法,因?yàn)槭褂么朔椒ú话踩?,目前該方法已被棄用?/p>

l 使用 interrupt()方法中斷線程。

使用標(biāo)志位終止線程

使用標(biāo)志位終止線程就是定義一個(gè)boolean型的標(biāo)志位 ,在線程的run方法中根據(jù)這個(gè)標(biāo)志位是為true還是為false來判斷是否終止,這種情況多用于while循環(huán)中。

代碼如下:

class StopThread extends Thread {
 
//標(biāo)志位
 
private boolean flag = true;
 
@Override
 
public synchronized void run() {
 
while (flag) {
 
System.out.println(Thread.currentThread().getName()+"---我是子線程");
 
}
 
}
 
/**
 
* @methodDesc: 功能描述:(終止線程)
 
*/
 
public void stopThread() {
 
flag = false;
 
System.out.println(getName()+"線程被終止掉了");
 
}
 
}
 
/**
 
@classDesc: 功能描述:(演示終止線程效果)
 
*/
 
public class StopThreadDemo {
 
public static void main(String[] args) {
 
StopThread stopThread1 = new StopThread();
 
StopThread stopThread2 = new StopThread();
 
stopThread1.start();
 
stopThread2.start();
 
for (int i = 0; i < 50; i++) {
 
System.out.println("------我是主線程-----"+i);
 
if(i==30) {
 
stopThread1.stopThread();
 
stopThread2.stopThread();
 
}
 
}
 
}
 
}

使用 stop() 終止線程(不安全)

棄用stop()方法的原因:

調(diào)用 stop() 方法會(huì)立刻停止 run() 方法中剩余的全部任務(wù),包括在 catch 或 finally 語(yǔ)句中的,并拋出ThreadDeath異常,因此可能會(huì)導(dǎo)致任務(wù)執(zhí)行失敗。

使用interrupt方法中斷線程

使用 interrupt() 方法中斷線程時(shí)并不會(huì)立即終止線程,而是通知目標(biāo)線程,告訴它有人希望你終止。至于目標(biāo)線程收到通知后會(huì)如何處理,則完全由目標(biāo)線程自行決定。

以上是python爬蟲取消或終止線程的方法的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)頁(yè)標(biāo)題:python爬蟲取消或終止線程的方法
文章網(wǎng)址:http://chinadenli.net/article14/gicsge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、網(wǎng)站設(shè)計(jì)公司、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、微信公眾號(hào)、定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)