創(chuàng)新互聯(lián)www.cdcxhl.cn八線動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買多久送多久,劃算不套路!
這篇文章給大家介紹利用java如何實(shí)現(xiàn)一個(gè)多線程,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
java 多線程詳解
在這篇文章里,我們關(guān)注多線程。多線程是一個(gè)復(fù)雜的話題,包含了很多內(nèi)容,這篇文章主要關(guān)注線程的基本屬性、如何創(chuàng)建線程、線程的狀態(tài)切換以及線程通信。
線程是操作系統(tǒng)運(yùn)行的基本單位,它被封裝在進(jìn)程中,一個(gè)進(jìn)程可以包含多個(gè)線程。即使我們不手動(dòng)創(chuàng)造線程,進(jìn)程也會有一個(gè)默認(rèn)的線程在運(yùn)行。
對于JVM來說,當(dāng)我們編寫一個(gè)單線程的程序去運(yùn)行時(shí),JVM中也是有至少兩個(gè)線程在運(yùn)行,一個(gè)是我們創(chuàng)建的程序,一個(gè)是垃圾回收。
線程基本信息
我們可以通過Thread.currentThread()方法獲取當(dāng)前線程的一些信息,并對其進(jìn)行修改。
我們來看以下代碼:
查看并修改當(dāng)前線程的屬性 String name = Thread.currentThread().getName(); int priority = Thread.currentThread().getPriority(); String groupName = Thread.currentThread().getThreadGroup().getName(); boolean isDaemon = Thread.currentThread().isDaemon(); System.out.println("Thread Name:" + name); System.out.println("Priority:" + priority); System.out.println("Group Name:" + groupName); System.out.println("IsDaemon:" + isDaemon); Thread.currentThread().setName("Test"); Thread.currentThread().setPriority(Thread.MAX_PRIORITY); name = Thread.currentThread().getName(); priority = Thread.currentThread().getPriority(); groupName = Thread.currentThread().getThreadGroup().getName(); isDaemon = Thread.currentThread().isDaemon(); System.out.println("Thread Name:" + name); System.out.println("Priority:" + priority);
本文標(biāo)題:利用java如何實(shí)現(xiàn)一個(gè)多線程-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://aaarwkj.com/article16/idogg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、微信公眾號、定制網(wǎng)站、App開發(fā)、網(wǎng)站設(shè)計(jì)公司、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容