仓酷云
标题:
JAVA网站制作之Java Thread Programming 1.8.1 - Inte...
[打印本页]
作者:
爱飞
时间:
2015-1-18 11:52
标题:
JAVA网站制作之Java Thread Programming 1.8.1 - Inte...
java比较简单,没有C++的烦琐,但学习时最好有C++为基础.与JSP和SQL起应用,功能强大.TheNeedforInter-threadSignaling
Throughsynchronization,onethreadcansafelychangevaluesthatanotherthreadwillread.Howdoesthesecondthreadknowthatthevalueshavechanged?Whatifthesecondthreadiswaitingforthevaluestochangebyrereadingthevalueseveryfewseconds?
Onenot-so-goodwaythatathreadcanwaitforavaluetochangeisbyusingabusy/wait:
while(getValue()!=desiredValue){
Thread.sleep(500);
}
Suchcodeiscalledabusy/waitbecausethethreadisbusyusingupprocessorresourcestocontinuallychecktoseeifthevaluehaschanged.Tousefewerresources,thesleeptimecouldbeincreased,butthenthethreadmightnotfindoutaboutthechangeforquitesometime.Ontheotherhand,ifthesleeptimeisreduced,thethreadwillfindoutsooner,butwillwasteevenmoreoftheprocessorresources.InJava,thereisamuchbetterwaytohandlethiskindofsituation:thewait/notifymechanism.
偶然候我们必要线程间的通信,好比第二个线程怎样晓得第一个线程的某些值产生了改动?不太好的办法如上,称之为busy/wait,经由过程不休轮回并分离Thread.sleep()测试值是不是产生变更,会占用途理器资本,而且轮回的频次不简单把握,快了华侈资本,慢了下降反响速率。像这类情形,java中给出了一种更好的办理办法:wait/notify机制
TheWait/NotifyMechanism
Thewait/notifymechanismallowsonethreadtowaitforanotificationfromanotherthreadthatitmayproceed.
MinimalWait/Notify
Atabareminimum,youneedanobjecttolockonandtwothreadstoimplementthewait/notifymechanism.
Imaginethatthereisamembervariable,valueLock,thatwillbeusedforsynchronization:
privateObjectvalueLock=newObject();
Thefirstthreadcomesalongandexecutesthiscodefragment:
synchronized(valueLock){
try{
valueLock.wait();
}catch(InterruptedExceptionx){
System.out.println(“interruptedwhilewaiting”);
}
}
Thewait()methodrequiresthecallingthreadtohavepreviouslyacquiredtheobject-levellockonthetargetobject.Inthiscase,theobjectthatwillbewaiteduponisvalueLock,andtwolinesbeforethevalueLock.wait()statementisthesynchronized(valueLock)statement.Thethreadthatinvokesthewait()methodreleasestheobject-levellockandgoestosleepuntilnotifiedorinterrupted.Ifthewaitingthreadisinterrupted,itcompeteswiththeotherthreadstoreacquiretheobject-levellockandthrowsanInterruptedExceptionfromwithinwait().Ifthewaitingthreadisnotified,itcompeteswiththeotherthreadstoreacquiretheobject-levellockandthenreturnsfromwait().
Wait()办法必要在取得object-levellock以后才干挪用,不然会抛出IllegalMonitor-
StateException非常,当线程挪用wait()办法后,会开释object-levellock,然后sleep(),直到被notified或interrupted,假如被interrupted,此线程会从头合作取得object-levellock,然后抛出InterrruptedException,假如被notified,此线程会从头合作取得object-levellock,从wait()前往,持续实行wait()今后的代码。
Manytimes,athreadisinterruptedtosignalitthatitshouldcleanupanddie(seeChapter5).ThestatementsusedtowaitcanbeslightlyrearrangedtoallowtheInterruptedExceptiontopropagateupfurther:
try{
synchronized(valueLock){
valueLock.wait();
}
}catch(InterruptedExceptionx){
System.out.println(“interruptedwhilewaiting”);
//cleanup,andallowthreadtoreturnfromrun()
}
偶然候,中止一个线程是为了将其烧毁扫除,能够将InterruptedException非常提早处置
InsteadofcatchingInterruptedException,methodscansimplydeclarethattheythrowittopasstheexceptionfurtherupthecallchain:
publicvoidsomeMethod()throwsInterruptedException{
//...
synchronized(valueLock){
valueLock.wait();
}
//...
}
假如不在办法内捕获非常,能够持续上抛,留待今后处置
Thethreaddoingthenotificationcomesalongandexecutesthiscodefragment:
synchronized(valueLock){
valueLock.notify();//notifyAll()mightbesafer...
}
Thisthreadblocksuntilitcangetexclusiveaccesstotheobject-levellockforvalueLock.Afterthelockisacquired,thisthreadnotifiesoneofthewaitingthreads.Ifnothreadsarewaiting,thenotificationeffectivelydoesnothing.IfmorethanonethreadiswaitingonvalueLock,thethreadschedulerarbitrarilychoosesonetoreceivethenotification.Theotherwaitingthreadsarenotnotifiedandcontinuetowait.Tonotifyallwaitingthreads(insteadofjustoneofthem),usenotifyAll()(discussedlaterinthischapter).
起首要取得object-levellock,然后挪用notify()关照此object-levellock上一切守候线程中的一个,假如没有waiting线程,则关照有效。假如有多个守候线程,则线程挪用机制选择个中一个关照,别的没有取得关照的持续守候。假如要关照一切此锁上的守候线程,合用notifyAll()
TypicalWait/Notify
Inmostcases,amembervariableischeckedbythethreaddoingthewaitingandmodifiedbythethreaddoingthenotification.Thecheckingandmodificationoccurinsidethesynchronizedblockstobesurethatnoraceconditionsdevelop.
年夜部分情形下,都是一个线程守候一个成员变量满意某个前提,另外一个线程修正此成员变量落后行关照。
Thistime,twomembervariablesareused:
privatebooleanvalue=false;
privateObjectvalueLock=newObject();
Thevaluevariableischeckedbythethreaddoingthewaitingandissetbythethreaddoingthenotification.SynchronizationonvalueLockcontrolsconcurrentaccesstovalue.
Thefirstthreadcomesalongandexecutesthiscodefragment:
try{
synchronized(valueLock){
while(value!=true){
valueLock.wait();
}
//valueisnowtrue
}
}catch(InterruptedExceptionx){
System.out.println(“interruptedwhilewaiting”);
}
Afteracquiringtheobject-levellockforvalueLock,thefirstthreadchecksvaluetoseeifitistrue.Ifitisnot,thethreadexecuteswait(),releasingtheobject-levellock.Whenthisthreadisnotified,itwakesup,reacquiresthelock,andreturnsfromwait().Tobesurethatitwasnotfalselynotified(seethe“EarlyNotification”discussionlaterinthischapter),itre-evaluatesthewhileexpression.Ifvalueisstillnottrue,thethreadwaitsagain.Ifitistrue,thethreadcontinuestoexecutetherestofthecodeinsidethesynchronizedblock.
Whilethefirstthreadiswaiting,asecondthreadcomesalongandexecutesthiscodefragment:
synchronized(valueLock){
value=true;
valueLock.notify();//notifyAll()mightbesafer...
}
Whenthefirstthreadexecutesthewait()methodonvalueLock,itreleasestheobject-levellockitwasholding.Thisreleaseallowsthesecondthreadtogetexclusiveaccesstotheobject-levellockonvalueLockandenterthesynchronizedblock.Inside,thesecondthreadsetsvaluetotrueandinvokesnotify()onvalueLocktosignalonewaitingthreadthatvaluehasbeenchanged.
Wait/NotifywithsynchronizedMethods
Sometimes,theclassisdesignedtosynchronizeonthisinsteadofanotherobject.Inthiscase,thesynchronizedmethodmodifiercanbeusedinsteadofasynchronizedstatement.Thefollowingcodefragmentsareanadaptationofthepreviousexample.
假如不是同步别的对象,而是同步this,能够类界说中分离wait/notify机制利用synchronizedmethod。
Asbefore,amembervariablevalueisinitiallysettofalse:
privatebooleanvalue=false;
Thefirstthread(threadA)comesalongandinvokesthiswaitUntilTrue()method:
publicsynchronizedvoidwaitUntilTrue()
throwsInterruptedException{
while(value==false){
wait();
}
}
WhilethreadAisblockedonthewait(),asecondthread(threadB)comesalongandexecutesthismethod,passingintruefornewValue:
publicsynchronizedvoidsetValue(booleannewValue){
if(newValue!=value){、
value=newValue;
notify();//notifyAll()mightbesafer...
}
}
Notethatbothmethodsaresynchronizedandaremembersofthesameclass.Inaddition,boththreadsareinvokingmethodsonthesameinstanceofthisclass.ThewaitUntilTrue()method(withthewait()inside)declaresthatitmightthrowanInterruptedException.Inthiscase,whenthreadBpassesintrue,valueischangedandnotify()isusedtosignalthewaitingthreadAthatitmayproceed.threadAwakesup,reacquirestheobject-levellockonthis,returnsfromwait(),andre-evaluatesthewhileexpression.Thistime,valueistrue,andthreadAwillreturnfromwaitUntilTrue().
如上,类界说中触及到wait和notify的两个办法都被界说成synchronized,假如两个线程都是挪用此类的统一个实例,则两个线程间能够相互通讯。
ObjectAPIUsedforWait/Notify
Thewait/notifymechanismisembeddeddeepintheheartofJava.Object,thesuperclassofallclasses,hasfivemethodsthatarethecoreofthewait/notifymechanism:notify(),notifyAll(),wait(),wait(long),andwait(long,int).AllclassesinJavainheritfromObject,soallclasseshavethesepublicmethodsavailabletothem.Additionally,noneofthesemethodscanbeoverriddeninasubclassastheyarealldeclaredfinal.
Java中一切类的父类Object内置了wait/notify机制,它内置wait/notify机制的五种中心办法:
notify(),notifyAll(),wait(),wait(long),wait(long,int),以是java中的一切类都具有这五种办法,这五种办法是public,而且final,不克不及被子类掩盖。
notify()
publicfinalnativevoidnotify()
throwsIllegalMonitorStateException//RuntimeException
Thenotify()methodisusedbyathreadtosignalanyotherthreadsthatmightbewaitingontheobject.Ifmorethanonethreadiswaitingontheobject,thethreadschedulerwillarbitrarilychooseexactlyonetobenotified,andtheotherswillcontinuetowait.Ifnothreadsarecurrentlywaitingontheobject,notify()hasnoeffect.Beforeinvokingnotify(),athreadmustgetexclusiveaccesstotheobject-levellockfortheobject.Unlikewait(),theinvocationofnotify()doesnottemporarilyreleasethelock.Iftheproperlockisnotheldwhennotify()iscalled,anIllegalMonitorStateExceptionisthrown.ThisexceptionisasubclassofRuntimeException,soatry-catchconstructisnotnecessaryandisrarelyused.
notify()办法用来关照在其旌旗灯号量上守候的一切别的线程。假如有一个以上的线程守候,则会选择个中一个举行关照,别的持续守候,假如没有线程守候,则此办法有效。挪用notify()其实不象wait()那样开释锁,而是守候锁完成后本人开释。假如挪用notify()是没有持有得当的锁,会抛出IllegalMonitorStateException(RuntimeException的子类,不用try/catch)
notifyAll()
publicfinalnativevoidnotifyAll()
throwsIllegalMonitorStateException//RuntimeException
ThenotifyAll()methodworksthesameasnotify()(seeabove)withoneimportantexception:WhennotifyAll()isinvoked,allthethreadswaitingontheobjectarenotified,notjustone.TheadvantageofnotifyAll()isthatyoudon’thavetobeconcernedaboutwhichoneofthewaitingthreadswillbenotified―theywillallbenotified.Thedisadvantageisthatitmightbewasteful(intermsofprocessorresources)tonotifyallthewaitingthreadsifonlyonewillactuallybeabletoproceed.Whenindoubt,erronthesideofsafetyoverspeedandusenotifyAll()insteadofnotify().
notifyAll()关照锁上的一切守候线程,其弱点不言而喻,假如没有需要关照一切守候线程,大概会华侈处置器资本。基于平安而不是速率思索,应当利用notifyAll(),而不是notify()。
wait()
publicfinalvoidwait()
throwsInterruptedException,
IllegalMonitorStateException//RuntimeException
Thewait()methodisusedtoputthecurrentthreadtosleepuntilitisnotifiedorinterrupted.Beforeinvokingwait(),athreadmustgetexclusiveaccesstotheobject-levellockfortheobject.Justafterenteringwait(),thecurrentthreadreleasesthelock.Beforereturningfromwait(),thethreadcompeteswiththeotherthreadstoreacquirethelock.Iftheproperlockisnotheldwhenwait()iscalled,anIllegalMonitorStateExceptionisthrown.ThisexceptionisasubclassofRuntimeException,soatry-catchconstructisnotnecessaryandisrarelyused.
Ifthewaitingthreadisinterrupted,itcompetestoreacquirethelockandthrowsanInterruptedExceptionfromwithinwait().ThisexceptionisnotasubclassofRuntimeException,soatry-catchconstructisrequired.
wait()将以后线程sleep直至被notified或interrupted。
挪用wait()之前,线程必需排他取得对象的object-levellock。
一进进wati(),以后线程就开释object-levellock。
在wait()前往之前,线程从头和别的线程合作取得此锁。
假如没有持有得当的锁就挪用了wait()会抛出IllegalMonitorStateException。
假如守候的线程被interrrupted,会从头取得此锁,然后抛出InterrruptedException,此非常非RuntimeException的子类,以是必需要try/catch。
wait(long)
publicfinalnativevoidwait(longmsTimeout)
throwsInterruptedException,
IllegalMonitorStateException,//RuntimeException
IllegalArgumentException//RuntimeException
Thewait(long)methodisusedtoputthecurrentthreadtosleepuntilitisnotified,interrupted,orthespecifiedtimeoutelapses.Otherthanthetimeout,wait(long)behavesthesameaswait()(seeabove).TheargumentmsTimeoutspecifiesthemaximumnumberofmillisecondsthatthethreadshouldwaitfornotification.IfmsTimeoutis0,thethreadwillnevertimeout(justlikewait()).Iftheargumentislessthan0,anIllegalArgumentExceptionwillbethrown.IllegalArgumentExceptionisasubclassofRuntimeException,soatry-catchblockisnotrequiredandisrarelyused.
让线程守候必定的工夫,毫秒,假如参数设为0,则无工夫限定,假如参数小于0,抛出IllegalArgumentException非常,此非常为RuntimeException的子类,不必要try-catch
Ifthespecifiednumberofmillisecondselapsesbeforethewaitingthreadisnotifiedorinterrupted,itcompetestoreacquirethelockandreturnsfromwait(long).Thereisnowayforthecallertodeterminewhetheranotificationoratimeoutoccurredbecausenoinformation(void)isreturnedfromwait(long).
假如设定的工夫内守候线程没有被notified/interrupted,会从头取得锁然后从wait()前往。挪用者没有举措晓得是被关照仍是超时,由于wait(long)没有前往值。
wait(long,int)
publicfinalvoidwait(longmsTimeout,intnanoSec)
throwsInterruptedException,
IllegalMonitorStateException,//RuntimeException
IllegalArgumentException//RuntimeException
Thewait(long,int)methodworksjustlikewait(long,int)(seeabove)withtheexceptionthatnanosecondscanbeaddedtothetimeoutvalue.TheargumentnanoSecisaddedtomsTimeouttodeterminethetotalamountoftimethatthethreadwillwaitfornotificationbeforereturning.Ananosecondisone-billionthofasecond(10E-9),andmostcommonimplementationsoftheJavaVMdon’ttrulysupportthisfinearesolutionoftime.Forthisreason,theuseofthemethodiscurrentlyquiterare.
同,wait(long),不外工夫设置更准确,可设置纳秒数,不外一样平常假造机未完成此办法。
WhentoUsenotifyAll()Insteadofnotify()
Thefundamentaldifferencebetweennotify()andnotifyAll()isthatifmorethanonethreadissimultaneouslywaitingfornotification,notify()willprovidenotificationtoonlyoneofthewaitingthreads,whereasnotifyAll()willprovidenotificationtoallofthem.Ifyourcodeiswelldefendedagainstearlynotifications(discussedlater),notifyAll()isgenerallythebetterchoice.
ThemajordisadvantageofnotifyAll()isthatitiswastefulofprocessorresourcesifallbutoneofthenotifiedthreadswillendupwaitingagain.Thisisasituationthatisdifficulttoguarantee.Ifyourcodesynchronizesonthiseitherthroughsynchronizedblocksorthesynchronizedmethodmodifier,youcan’tbesurethatsomecodeexternaltotheclasswon’tsynchronizeandwaitonareferencetotheobject.Ifthathappens,notify()mightsignalthethreadrunningthatexternalcodeinsteadofthethreadthatyouintended.Considerasituationwhereyouhaveaclass,ClassX,withtwomethods:
统一个锁有分歧的加锁体例,类外部给this加锁,内部类还能够给此类的实例加锁,多个线程守候此锁,notify()纷歧定会关照到希冀的线程。
类ClassX中有以下两个synchronized办法,个中包含了wait/notify
publicsynchronizedvoidwaitUntilTrue()
throwsInterruptedException{
while(value==false){
wait();
}
}
publicsynchronizedvoidsetValue(booleannewValue){
if(newValue!=value){
value=newValue;
notify();//notifyAll()mightbesafer...
}
}
同时,有个内部类ClassY,天生ClassX的实例,也此实例上wait()
Inaddition,there’sanexternalclass,ClassY,withthiscodeinoneofitsmethods:
ClassXcx=newClassX();
cx.setValue(false);
//...
synchronized(cx){
cx.wait();//trouble
}
IfthreadAisrunninginsideClassY,itsynchronizesoncxandinvokeswait().IfthreadBinvokeswaitUntilTrue(),itisnowalsowaitingfornotification.IfthreadCinvokessetValue()andpassestrue(anewvalue),threadCwillonlynotifyonethreadbecausenotifyAll()wasn’tused.There’snowaytobesurewhetherthreadAorthreadBwillbenotified.Inthissituation,notifyAll()wouldhaveguaranteedthattheywouldbothbenotified.
此时假如有三个线程:threadA在ClassY运转,同步cx并守候;threadB挪用cx的waitUntilTrue()守候;threadC挪用cx的setValue(),它只能关照threadA,B中的一个,没无方法断定哪个被关照。notifyAll()包管两者都被关照。
Itisgenerallysafetousenotify()onlywhenyoucanguaranteethatonlyonethreadwilleverbewaitingfornotification.Thisisarelativelyunusualoccurrence.
只要一个线程时,利用notify()
Ifyou’renotsurewhetheryouneedtousenotify()ornotifyAll(),usenotifyAll().Itmightbealittlewasteful,butit’ssafer.
假如不断定利用哪个,就利用notifyAll()吧
还是要自己一点一点写代码,然后编译,改错再编译好那。还有最重要的是.net的编译环境非常好,你甚是不需要了解太多工具,对于简单的系统,你可以之了解一些语法就哦了。
作者:
若天明
时间:
2015-1-21 16:40
Java是一种计算机编程语言,拥有跨平台、面向对java
作者:
简单生活
时间:
2015-1-30 21:13
不过,每次的执行编译后的字节码需要消耗一定的时间,这同时也在一定程度上降低了 Java 程序的运行效率。
作者:
透明
时间:
2015-2-6 16:00
是一种简化的C++语言 是一种安全的语言,具有阻绝计算机病毒传输的功能
作者:
不帅
时间:
2015-2-7 08:21
如果要向java web方向发展也要吧看看《Java web从入门到精通》学完再到《Struts2.0入门到精通》这样你差不多就把代码给学完了。有兴趣可以看一些设计模块和框架的包等等。
作者:
仓酷云
时间:
2015-2-14 09:59
那么我书也看了,程序也做了,别人问我的问题我都能解决了,是不是就成为高手了呢?当然没那么简单,这只是万里长征走完了第一步。不信?那你出去接一个项目,你知道怎么下手吗,你知道怎么设计吗,你知道怎么组织人员进行开发吗?你现在脑子里除了一些散乱的代码之外,可能再没有别的东西了吧!
作者:
老尸
时间:
2015-2-20 14:37
设计模式是高级程序员真正掌握面向对象核心思想的必修课。设计模式并不是一种具体"技术",它讲述的是思想,它不仅仅展示了接口或抽象类在实际案例中的灵活应用和智慧
作者:
山那边是海
时间:
2015-2-26 03:13
是一种语言,用以产生「小应用程序(Applet(s))
作者:
乐观
时间:
2015-3-1 11:17
你就该学一学Servlet了。Servlet就是服务器端小程序,他负责生成发送给客户端的HTML文件。JSP在执行时,也是先转换成Servlet再运行的。虽说JSP理论上可以完全取代Servlet,这也是SUN推出JSP的本意,可是Servlet用来控制流程跳转还是挺方便的,也令程序更清晰。接下来你应该学习一下Javabean了,可能你早就看不管JSP在HTML中嵌Java代码的混乱方式了,这种方式跟ASP又有什么区别呢?
作者:
谁可相欹
时间:
2015-3-6 21:17
其实说这种话的人就如当年小日本号称“三个月拿下中国”一样大言不惭。不是Tomjava泼你冷水,你现在只是学到了Java的骨架,却还没有学到Java的精髓。接下来你得研究设计模式了。
作者:
愤怒的大鸟
时间:
2015-3-6 23:49
吧,现在很流行的Structs就是它的一种实现方式,不过Structs用起来实在是很繁,我们只要学习其精髓即可,我们完全可以设计自己的MVC结构。然后你再研究一下软件Refactoring (重构)和极限XP编程,相信你又会上一个台阶。 做完这些,你不如整理一下你的Java代码,把那些经典的程序和常见的应用整理出来,再精心打造一番,提高其重用性和可扩展性。你再找几个志同道合的朋友成立一个工作室吧
作者:
莫相离
时间:
2015-3-8 11:39
是一种为 Internet发展的计算机语言
作者:
小妖女
时间:
2015-3-17 01:22
是一种简化的C++语言 是一种安全的语言,具有阻绝计算机病毒传输的功能
作者:
精灵巫婆
时间:
2015-3-17 06:40
是一种将安全性(Security)列为第一优先考虑的语言
作者:
变相怪杰
时间:
2015-4-3 21:05
不过,每次的执行编译后的字节码需要消耗一定的时间,这同时也在一定程度上降低了 Java 程序的运行效率。
作者:
深爱那片海
时间:
2015-4-4 01:00
让你能够真正掌握接口或抽象类的应用,从而在原来的Java语言基础上跃进一步,更重要的是,设计模式反复向你强调一个宗旨:要让你的程序尽可能的可重用。
作者:
柔情似水
时间:
2015-4-5 11:09
你一定会高兴地说,哈哈,原来成为Java高手就这么简单啊!记得Tomjava也曾碰到过一个项目经理,号称Java很简单,只要三个月就可以学会。
作者:
爱飞
时间:
2015-4-5 12:20
还好,SUN提供了Javabean可以把你的JSP中的 Java代码封装起来,便于调用也便于重用。
作者:
再见西城
时间:
2015-4-10 11:36
科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。
欢迎光临 仓酷云 (http://ckuyun.com/)
Powered by Discuz! X3.2