挺底層的啊
創(chuàng)新互聯(lián)是專業(yè)的安龍網(wǎng)站建設(shè)公司,安龍接單;提供成都做網(wǎng)站、成都網(wǎng)站制作,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行安龍網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
1 取決于操作系統(tǒng),也是取決于操作系統(tǒng)
2 不知道。只知道notify() 和 notifyAll()
我是自己理解的,比如java線程要"基于優(yōu)先權(quán)"調(diào)度,給線程設(shè)置了優(yōu)先權(quán),但操作系統(tǒng)只支持"先來先服務(wù)"調(diào)度,java也不能改變系統(tǒng)的底層調(diào)度吧.
下面是java線程調(diào)度的具體說明,前面是中文翻譯(不保證準(zhǔn)確),后面是英文原著,希望對你有用:
在java技術(shù)中,線程通常是搶占式的而不需要時(shí)間片分配進(jìn)程(分配給每個(gè)線程相等的cpu時(shí)間的進(jìn)程)。一個(gè)經(jīng)常犯的錯(cuò)誤是認(rèn)為“搶占”就是“分配時(shí)間片”。
在Solaris平臺上的運(yùn)行環(huán)境中,相同優(yōu)先級的線程不能相互搶占對方的cpu時(shí)間。但是,在使用時(shí)間片的windows平臺運(yùn)行環(huán)境中,可以搶占相同甚至更高優(yōu)先級的線程的cpu時(shí)間。搶占并不是絕對的,可是大多數(shù)的JVM的實(shí)現(xiàn)結(jié)果在行為上表現(xiàn)出了嚴(yán)格的搶占??v觀JVM的實(shí)現(xiàn),并沒有絕對的搶占或是時(shí)間片,而是依賴于編碼者對wait和sleep這兩個(gè)方法的使用。
搶占式調(diào)度模型就是許多線程屬于可以運(yùn)行狀態(tài)(等待狀態(tài)),但實(shí)際上只有一個(gè)線程在運(yùn)行。該線程一直運(yùn)行到它終止進(jìn)入可運(yùn)行狀態(tài)(等待狀態(tài))或是另一個(gè)具有更高優(yōu)先級的線程變成可運(yùn)行狀態(tài)。在后一種情況下,底優(yōu)先級的線程被高優(yōu)先級的線程搶占,高優(yōu)先級的線程獲得運(yùn)行的機(jī)會(huì)。
線程可以因?yàn)楦鞣N各樣的原因終止并進(jìn)入可運(yùn)行狀態(tài)(因?yàn)槎氯?。例如,線程的代碼可以在適當(dāng)時(shí)候執(zhí)行Thread.sleep()方法,故意讓線程中止;線程可能為了訪問資源而不得不等待直到該資源可用為止。
所有可運(yùn)行的線程根據(jù)優(yōu)先級保持在不同的池中。一旦被堵塞的線程進(jìn)入可運(yùn)行狀態(tài),它將會(huì)被放回適當(dāng)?shù)目蛇\(yùn)行池中。非空最高優(yōu)先級的池中的線程將獲得cpu時(shí)間。
最后一個(gè)句子是不精確的,因?yàn)椋?/p>
(1)在大多數(shù)的JVM實(shí)現(xiàn)中,雖然不能保證說優(yōu)先級有任何意義,但優(yōu)先級看起來象是用搶占方式工作。
(2)微軟windows的評價(jià)影響線程的行為,以至盡管一個(gè)處于可運(yùn)行狀態(tài)的優(yōu)先級為5的java線程正在等待cpu時(shí)間,但是一個(gè)優(yōu)先級為4的java線程卻可能正在運(yùn)行。
實(shí)際上,許多JVM用隊(duì)列來實(shí)現(xiàn)池,但沒有保證行為。
Thread Scheduling
In Java technology,threads are usually preemptive,but not necessarily Time-sliced(the
process of giving each thread an equal amount of
CPU time).It is common mistake to believe that
"preemptive" is a fancy word for "does time-slicing".
For the runtime on a Solaris Operating Environment platform,Java technology does not preempt threads of the same priority.However,the runtime on Microsoft Windows platforms uses time-slicing,so it preempts threads of the same priority and even threads of higher priority.Preemption is not
guaranteed;however,most JVM implementations result in behavior that appears to be strictly preemptive.Across JVM implementations,there is
no absolute guarantee of preemption or
time-slicing.The only guarantees lie in the
coder’s use of wait and sleep. The model of a preemptive scheduler is that many threads might be runnable,but only one thread is actually running.This thread continues to run until it ceases to be runnable or another thread of higher priority becomes runnable.In the latter case,the lower priority thread is preempted by the thread of higher priority,which gets a chance to run instead.
A thread might cease to runnable (that is,because blocked) for a variety of reasons.The thread’s code can execute a Thread.sleep() call,deliberately asking the thread to pause for a fixed period of time.The thread might have to wait to access a resource and cannot continue until that resource become available.
All thread that are runnable are kept in pools
according to priority.When a blocked thread
becomes runnable,it is placed back into the
appropriate runnable pool.Threads from the
highest priority nonempty pool are given CPU time.
The last sentence is worded loosed because:
(1) In most JVM implementations,priorities seem
to work in a preemptive manner,although there is
no guarantee that priorities have any meaning at all;
(2) Microsoft Window’s values affect thread
behavior so that it is possible that a Java
Priority 4 thread might be running,in spite of
the fact that a runnable Java Priority 5 thread
is waiting for the CPU. In reality,many JVMs implement pool as queues,but this is not guaranteed hehavior.
cpu吧所有的時(shí)間分成相等的很小的時(shí)間片,每個(gè)進(jìn)程一次只能得到一個(gè)時(shí)間片去執(zhí)行,如果在這個(gè)時(shí)間片以內(nèi)時(shí)間內(nèi)完成任務(wù)那最好,如果還沒完成,則要先暫停然別的程序執(zhí)行,然后過了一會(huì)兒又輪到剛才的程序執(zhí)行,但是這種輪轉(zhuǎn)還是很快的,我們一般感覺不到
至于java如何實(shí)現(xiàn),不知道,就像樓上說的,應(yīng)該是由CPU根據(jù)時(shí)間片輪轉(zhuǎn)算法確定的
這個(gè)時(shí)間片是操作系統(tǒng)自己分配的,程序員只需要決定是否啟動(dòng)線程和終止線程就可以了。時(shí)間片是并行操作系統(tǒng)中的一個(gè)概念,意思是將時(shí)間分成多個(gè)小片段根據(jù)線程優(yōu)先級采用某些策略以此給不同的線程分配時(shí)間片段,從用戶的角度來看線程好像是在同時(shí)運(yùn)行一樣?,F(xiàn)在采用多核系統(tǒng)可以真正的同時(shí)運(yùn)行多個(gè)線程了,這里面的分配機(jī)制就更加復(fù)雜了。寫java代碼操作不到這么底層的東西。
本文名稱:java代碼時(shí)間片,java線程時(shí)間片
轉(zhuǎn)載注明:http://chinadenli.net/article38/heegpp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、響應(yīng)式網(wǎng)站、面包屑導(dǎo)航、定制網(wǎng)站、虛擬主機(jī)、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(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)