仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1527|回复: 18
打印 上一主题 下一主题

[学习教程] JAVA网页编程之Java Thread Programming 1.8.2 - Inte...

[复制链接]
愤怒的大鸟 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-18 11:54:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
前些天,在CSDN上看到了一个消息,说是ASP.NETAJAX成功在Linux上运行,这一点对我触动很大,而且引发了我许多感叹,所以想写出来分享一下。MissedNotificationAmissednotificationoccurswhenthreadBtriestonotifythreadA,butthreadAisnotyetwaitingforthenotification.InamultithreadedenvironmentlikeJava,youdon’thavemuchcontroloverwhichthreadrunsandforhowlong.Thisuncertaintycanleadtoasituationinwhichmostofthetimeanapplicationisrun,threadAiswaitingbeforethreadBdoesthenotification.Butoccasionally,threadBdoesthenotificationbeforethreadAiswaiting.Thismissednotificationscenariocanbequitedangerous.MissedNotification指:线程B试图关照线程A,但线程A并没有在守候关照。这并非不成能呈现的。在多线程情况中,我们不克不及把持哪一个线程实行,实行多长工夫,这类不断定有大概招致在一个线程守候之前就先行关照,这是一种很伤害的情形。以下程序会呈现这类情形:/**Createdon2005-7-14**JavaThreadProgramming-PaulHyde*Copyright?1999SamsPublishing*JonathanQ.Bo进修条记**/packageorg.tju.msnrl.jonathan.thread.chapter8;/***@authorJonathanQ.BofromTJUMSNRL**Email:jonathan.q.bo@gmail.com*Blog:blog.csdn.net/jonathan_q_bo*blog.yesky.net/jonathanundersun**EnjoyLifewithSun!**/publicclassMissedNotify{privateObjectproceedLock;publicMissedNotify(){proceedLock=newObject();}publicvoidwaitProceed()throwsInterruptedException{print("inwaitProceed()-begin");synchronized(proceedLock){print("beginsynchronizedwait...");proceedLock.wait();print("endsynchronizedwait...");}print("inwaitProceed()-end");}publicvoidnotifyProceed(){print("innotifyProceed()-begin");synchronized(proceedLock){print("beginsynchronizednotify...");proceedLock.notifyAll();print("endsynchronizednotify...");}print("innotifyProceed()-end");}publicstaticvoidprint(Stringmsg){Stringtemp=Thread.currentThread().getName();System.out.println(temp+"-"+msg);}publicstaticvoidmain(String[]args){finalMissedNotifymn=newMissedNotify();RunnablerunA=newRunnable(){publicvoidrun(){try{Thread.sleep(1000);//wait()后实行mn.waitProceed();}catch(InterruptedExceptione){e.printStackTrace();}}};ThreadthreadA=newThread(runA,"threadA");threadA.start();RunnablerunB=newRunnable(){publicvoidrun(){try{Thread.sleep(500);//notify()先实行mn.notifyProceed();}catch(InterruptedExceptione){e.printStackTrace();}}};ThreadthreadB=newThread(runB,"threadB");threadB.start();try{Thread.sleep(10000);}catch(InterruptedExceptione){}print("interruptthreadA...");threadA.interrupt();}}输入了局:threadB-innotifyProceed()-beginthreadB-beginsynchronizednotify...threadB-endsynchronizednotify...threadB-innotifyProceed()-endthreadA-inwaitProceed()-beginthreadA-beginsynchronizedwait...main-interruptthreadA...java.lang.InterruptedExceptionatjava.lang.Object.wait(NativeMethod)atjava.lang.Object.wait(Object.java:429)atorg.tju.msnrl.jonathan.thread.chapter8.MissedNotify.waitProceed(MissedNotify.java:35)atorg.tju.msnrl.jonathan.thread.chapter8.MissedNotify$1.run(MissedNotify.java:66)atjava.lang.Thread.run(Thread.java:534)办理办法:只需加一个标记位:/**Createdon2005-7-14**JavaThreadProgramming-PaulHyde*Copyright?1999SamsPublishing*JonathanQ.Bo进修条记**/packageorg.tju.msnrl.jonathan.thread.chapter8;/***@authorJonathanQ.BofromTJUMSNRL**Email:jonathan.q.bo@gmail.com*Blog:blog.csdn.net/jonathan_q_bo*blog.yesky.net/jonathanundersun**EnjoyLifewithSun!**/publicclassMissedNotifyFix{privateObjectproceedLock;privatebooleanokToProceed;publicMissedNotifyFix(){okToProceed=false;proceedLock=newObject();}publicvoidwaitProceed()throwsInterruptedException{print("inwaitProceed()-begin");synchronized(proceedLock){while(!okToProceed){print("beginsynchronizedwait...");proceedLock.wait();print("endsynchronizedwait...");}}print("inwaitProceed()-end");}publicvoidnotifyProceed(){print("innotifyProceed()-begin");synchronized(proceedLock){okToProceed=true;print("beginsynchronizednotify...");proceedLock.notifyAll();print("endsynchronizednotify...");}print("innotifyProceed()-end");}publicstaticvoidprint(Stringmsg){Stringtemp=Thread.currentThread().getName();System.out.println(temp+"-"+msg);}publicstaticvoidmain(String[]args){finalMissedNotifyFixmn=newMissedNotifyFix();RunnablerunA=newRunnable(){publicvoidrun(){try{Thread.sleep(1000);mn.waitProceed();}catch(InterruptedExceptione){e.printStackTrace();}}};ThreadthreadA=newThread(runA,"threadA");threadA.start();RunnablerunB=newRunnable(){publicvoidrun(){try{Thread.sleep(500);mn.notifyProceed();}catch(InterruptedExceptione){e.printStackTrace();}}};ThreadthreadB=newThread(runB,"threadB");threadB.start();try{Thread.sleep(10000);}catch(InterruptedExceptione){}print("interruptthreadA...");threadA.interrupt();}}输入了局:threadB-innotifyProceed()-beginthreadB-beginsynchronizednotify...threadB-endsynchronizednotify...threadB-innotifyProceed()-endthreadA-inwaitProceed()-beginthreadA-inwaitProceed()-endmain-interruptthreadA...EarlyNotificationIfathreadisnotifiedwhilewaiting,buttheconditionthethreadiswaitingforhasnotyetbeenmet,thethreadhasreceivedanearlynotification.Anearlynotificationcanalsooccuriftheconditionisbrieflymetbutquicklychangessoit’snolongermet.Thismightsoundstrange,butearlynotificationcanhappenduetosubtleerrorsinthecode(generallywhenanifisusedinsteadofawhile).Earlynotification是指:当一个守候线程被关照的时分,它守候的前提不再满意,这时候我们说,线程收到了一个earlynotification。Earlynotification在守候的前提刹时满意然后立即改动不再满意时,也会产生。一般在一个必要利用while的中央利用了if。Earlynotification的代码:/**Createdon2005-7-14**JavaThreadProgramming-PaulHyde*Copyright?1999SamsPublishing*JonathanQ.Bo进修条记**/packageorg.tju.msnrl.jonathan.thread.chapter8;importjava.util.*;/***@authorJonathanQ.BofromTJUMSNRL**Email:jonathan.q.bo@gmail.com*Blog:blog.csdn.net/jonathan_q_bo*blog.yesky.net/jonathanundersun**EnjoyLifewithSun!**/publicclassEarlyNotify{privateListlist;publicEarlyNotify(){list=Collections.synchronizedList(newLinkedList());}publicStringremoveItem()throwsInterruptedException{print("enteringremoveItem()...");synchronized(list){if(list.isEmpty()){print("listwait...begin");list.wait();print("listwait...end");}Stringitem=(String)list.remove(0);print("leaveingremoveItem()...");return"youremove"+item;}}publicvoidaddItem(Stringitem){print("enteringaddItem()...");synchronized(list){list.add(item);print("listnotify...begin");list.notifyAll();print("listnotify...end");}print("leaveaddItem()...");}publicstaticvoidprint(Stringmsg){Stringtemp=Thread.currentThread().getName();System.out.println(temp+"-"+msg);}publicstaticvoidmain(String[]args){finalEarlyNotifyen=newEarlyNotify();RunnablerunA=newRunnable(){publicvoidrun(){try{Stringitem=en.removeItem();print("inrun()removeitem"+item);}catch(InterruptedExceptione1){print("interrupted");}catch(Exceptione2){print("exception"+e2.getMessage());}}};RunnablerunB=newRunnable(){publicvoidrun(){en.addItem("hello");}};try{ThreadthreadA=newThread(runA,"threadA");threadA.start();Thread.sleep(500);ThreadthreadB=newThread(runA,"threadB");threadB.start();Thread.sleep(500);ThreadthreadC=newThread(runB,"threadC");threadC.start();Thread.sleep(10000);threadA.interrupt();threadB.interrupt();}catch(InterruptedExceptione1){}catch(Exceptione2){}}}输入了局:threadA-enteringremoveItem()...threadA-listwait...beginthreadB-enteringremoveItem()...threadB-listwait...beginthreadC-enteringaddItem()...threadC-listnotify...beginthreadC-listnotify...endthreadA-listwait...endthreadA-leaveingremoveItem()...threadA-inrun()removeitemyouremovehellothreadB-listwait...endthreadC-leaveaddItem()...threadB-exceptionIndex:0,Size:0准确的代码:/**Createdon2005-7-14**JavaThreadProgramming-PaulHyde*Copyright?1999SamsPublishing*JonathanQ.Bo进修条记**/packageorg.tju.msnrl.jonathan.thread.chapter8;importjava.util.*;/***@authorJonathanQ.BofromTJUMSNRL**Email:jonathan.q.bo@gmail.com*Blog:blog.csdn.net/jonathan_q_bo*blog.yesky.net/jonathanundersun**EnjoyLifewithSun!**/publicclassEarlyNotifyFix{privateListlist;publicEarlyNotifyFix(){list=Collections.synchronizedList(newLinkedList());}publicStringremoveItem()throwsInterruptedException{print("enteringremoveItem()...");synchronized(list){while(list.isEmpty()){//usewhileinsteadofifprint("listwait...begin");list.wait();print("listwait...end");}Stringitem=(String)list.remove(0);print("leaveingremoveItem()...");return"youremove"+item;}}publicvoidaddItem(Stringitem){print("enteringaddItem()...");synchronized(list){list.add(item);print("listnotify...begin");list.notifyAll();print("listnotify...end");}print("leaveaddItem()...");}publicstaticvoidprint(Stringmsg){Stringtemp=Thread.currentThread().getName();System.out.println(temp+"-"+msg);}publicstaticvoidmain(String[]args){finalEarlyNotifyFixen=newEarlyNotifyFix();RunnablerunA=newRunnable(){publicvoidrun(){try{Stringitem=en.removeItem();print("inrun()removeitem"+item);}catch(InterruptedExceptione1){print("interrupted");}catch(Exceptione2){print("exception"+e2.getMessage());}}};RunnablerunB=newRunnable(){publicvoidrun(){en.addItem("hello");}};try{ThreadthreadA=newThread(runA,"threadA");threadA.start();Thread.sleep(500);ThreadthreadB=newThread(runA,"threadB");threadB.start();Thread.sleep(500);ThreadthreadC=newThread(runB,"threadC");threadC.start();Thread.sleep(10000);threadA.interrupt();threadB.interrupt();}catch(InterruptedExceptione1){}catch(Exceptione2){}}}输入了局:threadA-enteringremoveItem()...threadA-listwait...beginthreadB-enteringremoveItem()...threadB-listwait...beginthreadC-enteringaddItem()...threadC-listnotify...beginthreadC-listnotify...endthreadA-listwait...endthreadA-leaveingremoveItem()...threadA-inrun()removeitemyouremovehellothreadB-listwait...endthreadB-listwait...beginthreadC-leaveaddItem()...threadB-interrupted
令人可喜的是java现在已经开源了,所以我想我上述的想法也许有一天会实现,因为java一直都是不断创新的语言,每次创新都会给我们惊喜,这也是我喜欢java的一个原因。
精灵巫婆 该用户已被删除
沙发
发表于 2015-1-21 16:56:51 | 只看该作者
是一种使网页(Web Page)由静态(Static)转变为动态(Dynamic)的语言
简单生活 该用户已被删除
板凳
发表于 2015-1-25 22:23:41 | 只看该作者
象、泛型编程的特性,广泛应用于企业级Web应用开发和移动应用开发。
小魔女 该用户已被删除
地板
发表于 2015-2-4 06:26:43 | 只看该作者
J2SE开发桌面应用软件比起 VC,VB,DEPHI这些传统开发语言来说,优势好象并不明显。J2ME对于初学者来说,好象又有点深奥,而且一般开发者很难有开发环境。
冷月葬花魂 该用户已被删除
5#
发表于 2015-2-4 21:24:36 | 只看该作者
其实说这种话的人就如当年小日本号称“三个月拿下中国”一样大言不惭。不是Tomjava泼你冷水,你现在只是学到了Java的骨架,却还没有学到Java的精髓。接下来你得研究设计模式了。
变相怪杰 该用户已被删除
6#
发表于 2015-2-6 11:31:15 | 只看该作者
我大二,Java也只学了一年,觉得还是看thinking in java好,有能力的话看英文原版(中文版翻的不怎么好),还能提高英文文档阅读能力。
乐观 该用户已被删除
7#
发表于 2015-2-8 19:49:47 | 只看该作者
Java是一个纯的面向对象的程序设计语言,它继承了 C++语言面向对象技术的核心。Java舍弃了C ++语言中容易引起错误的指针(以引用取代)、运算符重载(operator overloading)
山那边是海 该用户已被删除
8#
发表于 2015-2-9 00:26:53 | 只看该作者
你一定会高兴地说,哈哈,原来成为Java高手就这么简单啊!记得Tomjava也曾碰到过一个项目经理,号称Java很简单,只要三个月就可以学会。
爱飞 该用户已被删除
9#
发表于 2015-2-9 20:31:44 | 只看该作者
吧,现在很流行的Structs就是它的一种实现方式,不过Structs用起来实在是很繁,我们只要学习其精髓即可,我们完全可以设计自己的MVC结构。然后你再研究一下软件Refactoring (重构)和极限XP编程,相信你又会上一个台阶。 做完这些,你不如整理一下你的Java代码,把那些经典的程序和常见的应用整理出来,再精心打造一番,提高其重用性和可扩展性。你再找几个志同道合的朋友成立一个工作室吧
分手快乐 该用户已被删除
10#
发表于 2015-2-27 21:02:57 | 只看该作者
其实说这种话的人就如当年小日本号称“三个月拿下中国”一样大言不惭。不是Tomjava泼你冷水,你现在只是学到了Java的骨架,却还没有学到Java的精髓。接下来你得研究设计模式了。
小女巫 该用户已被删除
11#
发表于 2015-3-7 01:06:57 | 只看该作者
你快去找一份Java的编程工作来做吧(如果是在校学生可以去做兼职啊),在实践中提高自己,那才是最快的。不过你得祈祷在公司里碰到一个高手,而且他 还愿意不厌其烦地教你,这样好象有点难哦!还有一个办法就是读开放源码的程序了。我们知道开放源码大都出自高手,他们设计合理,考虑周到,再加上有广大的程序员参与,代码的价值自然是字字珠叽,铿锵有力(对不起,偶最近《金装四大才子》看多了)。
不帅 该用户已被删除
12#
发表于 2015-3-13 23:33:59 | 只看该作者
Java是一种计算机编程语言,拥有跨平台、面向对java
13#
发表于 2015-3-23 23:15:39 | 只看该作者
另外编写和运行Java程序需要JDK(包括JRE),在sun的官方网站上有下载,thinking in java第三版用的JDK版本是1.4,现在流行的版本1.5(sun称作J2SE 5.0,汗),不过听说Bruce的TIJ第四版国外已经出来了,是专门为J2SE 5.0而写的。
灵魂腐蚀 该用户已被删除
14#
发表于 2015-4-12 15:10:33 | 只看该作者
Jive的资料在很多网站上都有,大家可以找来研究一下。相信你读完代码后,会有脱胎换骨的感觉。遗憾的是Jive从2.5以后就不再无条件的开放源代码,同时有licence限制。不过幸好还有中国一流的Java程序员关注它,外国人不开源了,中国人就不能开源吗?这里向大家推荐一个汉化的Jive版本—J道。Jive(J道版)是由中国Java界大名 鼎鼎的banq在Jive 2.1版本基础上改编而成, 全中文,增加了一些实用功能,如贴图,用户头像和用户资料查询等,而且有一个开发团队在不断升级。你可以访问banq的网站
愤怒的大鸟 该用户已被删除
15#
 楼主| 发表于 2015-4-14 00:46:12 | 只看该作者
科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。
只想知道 该用户已被删除
16#
发表于 2015-4-14 22:52:07 | 只看该作者
[url]http://www.jdon.com/[/url]去下载,或到同济技术论坛的服务器[url]ftp://nro.shtdu.edu.cn[/url]去下,安装上有什么问题,可以到论坛上去提问。
再见西城 该用户已被删除
17#
发表于 2015-4-27 14:34:35 | 只看该作者
一般学编程语言都是从C语开始学的,我也不例外,但还是可能不学过程语言而直接学面向对象语言的,你是刚接触语言,还是从C开始学比较好,基础会很深点,如果你直接学习JAVA也能上手,一般大家在学语言的时候都记一些语言的关键词,常有的包和接口等。再去做逻辑代码的编写,以后的学习过程都是从逻辑代码编写中提升的,所以这方面都是经验积累的。你要开始学习就从
老尸 该用户已被删除
18#
发表于 2015-5-3 11:05:36 | 只看该作者
你一定会高兴地说,哈哈,原来成为Java高手就这么简单啊!记得Tomjava也曾碰到过一个项目经理,号称Java很简单,只要三个月就可以学会。
活着的死人 该用户已被删除
19#
发表于 2015-5-3 21:24:03 | 只看该作者
我大二,Java也只学了一年,觉得还是看thinking in java好,有能力的话看英文原版(中文版翻的不怎么好),还能提高英文文档阅读能力。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-10 13:16

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表