JavaÏß³Ì֪ʶÉîÈë½âÎö(2)
¶àÏ̳߳ÌÐò
¶ÔÓÚ¶àÏ̵߳ĺô¦Õâ¾Í²»¶à˵ÁË¡£µ«ÊÇ,ËüͬÑùÒ²´øÀ´ÁËijЩеÄÂé·³¡£Ö»ÒªÔÚÉè¼Æ³ÌÐòʱÌرðСÐÄÁôÒâ,¿Ë·þÕâЩÂé·³²¢²»ËãÌ«À§ÄÑ¡£
(1)ͬ²½Ïß³Ì
Ðí¶àÏß³ÌÔÚÖ´ÐÐÖбØÐ뿼ÂÇÓëÆäËûÏß³ÌÖ®¼ä¹²ÏíÊý¾Ý»òе÷Ö´ÐÐ״̬¡£Õâ¾Í ÐèҪͬ²½»úÖÆ¡£ÔÚJavaÖÐÿ¸ö¶ÔÏó¶¼ÓÐÒ»°ÑËøÓëÖ®¶ÔÓ¦¡£µ«Java²»Ìṩµ¥¶ÀµÄlockºÍunlock²Ù×÷¡£ËüÓɸ߲ãµÄ½á¹¹ÒþʽʵÏÖ, À´±£Ö¤²Ù×÷µÄ¶ÔÓ¦¡£(È»¶ø,ÎÒÃÇ×¢Òâµ½JavaÐéÄâ»úÌṩµ¥¶ÀµÄmonito renterºÍmonitorexitÖ¸ÁîÀ´ÊµÏÖlockºÍunlock²Ù×÷¡£)
synchronizedÓï¾ä¼ÆËãÒ»¸ö¶ÔÏóÒýÓÃ,ÊÔͼ¶Ô¸Ã¶ÔÏóÍê³ÉËø²Ù×÷, ²¢ÇÒÔÚÍê³ÉËø²Ù×÷Ç°Í£Ö¹´¦Àí¡£µ±Ëø²Ù×÷Íê³ÉsynchronizedÓï¾äÌåµÃµ½Ö´ÐС£µ±Óï¾äÌåÖ´ÐÐÍê±Ï(ÎÞÂÛÕý³£»òÒì³£),½âËø²Ù×÷×Ô¶¯Íê³É¡£×÷ΪÃæÏò¶ÔÏóµÄÓïÑÔ,synchronized¾³£Óë·½·¨Á¬Óá£Ò»ÖֱȽϺõİ취ÊÇ,Èç¹ûij¸ö±äÁ¿ÓÉÒ»¸öÏ̸߳³Öµ²¢ÓɱðµÄÏß³ÌÒýÓûò¸³Öµ,ÄÇôËùÓжԸñäÁ¿µÄ·ÃÎʶ¼±ØÐëÔÚij¸ösynchromizedÓï¾ä»òsynchronized·½·¨ÄÚ¡£
ÏÖÔÚ¼ÙÉèÒ»ÖÖÇé¿ö£ºÏß³Ì1ÓëÏß³Ì2¶¼Òª·ÃÎÊij¸öÊý¾ÝÇø,²¢ÇÒÒªÇóÏß³Ì1µÄ·ÃÎÊÏÈÓÚÏß³Ì2, ÔòÕâʱ½öÓÃsynchronizedÊDz»Äܽâ¾öÎÊÌâµÄ¡£ÕâÔÚUnix»òWindows NTÖпÉÓÃSimaphoreÀ´ÊµÏÖ¡£¶øJava²¢²»Ìṩ¡£ÔÚJavaÖÐÌṩµÄÊÇwait()ºÍnotify()»úÖÆ¡£Ê¹ÓÃÈçÏÂ:
synchronized method-1(…){ call by thread 1.
¡Îaccess data area;
available=true;
notify()
}
synchronized method-2(…){¡Îcall by thread 2.
while(!available)
try{
wait();¡Îwait for notify().
}catch (Interrupted Exception e){
} ¡Îaccess data area
}
ÆäÖÐavailableÊÇÀà³ÉÔ±±äÁ¿,ÖóõֵΪfalse¡£Èç¹ûÔÚmethod-2Öмì²éavailableΪ¼Ù,Ôòµ÷ÓÃwait()¡£wait()µÄ×÷ÓÃÊÇʹÏß
³Ì2½øÈë·ÇÔËÐÐ̬,²¢ÇÒ½âËø¡£ÔÚÕâÖÖÇé¿öÏÂ,method-1¿ÉÒÔ±»Ïß³Ì1µ÷Óᣵ±Ö´ÐÐ notify()ºó¡£Ïß³Ì2ÓÉ·ÇÔËÐÐ̬ת±äΪ¿ÉÔËÐÐ̬¡£µ±method-1µ÷Ó÷µ»Øºó¡£Ïß³Ì2 ¿ÉÖØжԸöÔÏó¼ÓËø,¼ÓËø³É¹¦ºóÖ´ÐÐwait()·µ»ØºóµÄÖ¸Áî¡£ÕâÖÖ»úÖÆÒ²ÄÜÊÊÓÃÓÚ ÆäËû¸ü¸´ÔÓµÄÇé¿ö¡£
(2)ËÀËø
Èç¹û³ÌÐòÖÐÓм¸¸ö¾ºÕù×ÊÔ´µÄ²¢·¢Ïß³Ì,ÄÇô±£Ö¤¾ùºâÊǺÜÖØÒªµÄ¡£ÏµÍ³¾ùºâÊÇָÿ¸öÏß³ÌÔÚÖ´Ðйý³ÌÖж¼Äܳä·Ö·ÃÎÊÓÐÏÞµÄ×ÊÔ´¡£ÏµÍ³ÖÐûÓжöËÀºÍËÀËøµÄÏ̡߳£Java²¢²»Ìṩ¶ÔËÀËøµÄ¼ì²â»úÖÆ¡£¶Ô´ó¶àÊýµÄJava³ÌÐòÔ±À´Ëµ·ÀÖ¹ËÀËøÊÇÒ»ÖֽϺõÄÑ¡Ôñ¡£×î¼òµ¥µÄ·ÀÖ¹ËÀËøµÄ·½·¨ÊǶԾºÕùµÄ×ÊÔ´ÒýÈëÐòºÅ,Èç¹ûÒ»¸öÏß³ÌÐèÒª¼¸¸ö×ÊÔ´,ÄÇôËü±ØÐëÏȵõ½Ð¡ÐòºÅµÄ×ÊÔ´,ÔÙÉêÇë´óÐ
Ïà¹ØÎĵµ£º
1. HttpSession session = request.getSession()
ÓëHttpSession session = request.getSession(true)µÄÇø±ð£¿
²Î¿¼´ð°¸£º
getSession(true)µÄº¯ÊýÔÐÍΪ:£ºHttpSession session = request.getSession (Boolean create)
Èç¹ûÓÐÓ뵱ǰµÄrequestÏȹØÁªµÄHttpSession,ÄÇô·µ»ØrequestÏà¹ØÁªµÄHttpS ......
³£³£Óöµ½Êý×éÅÅÐòµÄÎÊÌâ.±ÈÈçÎÒÓÐÒ»¸öPersonÀà,ËüµÄʵÀý¶ÔÏó´æ´¢ÔÚArrayListÊý×éÖÐ,ÏÖÔÚÒª°ÑArrayListÊý×éÖеÄPerson¶ÔÏó°´ÕÕÄêÁäÅÅÐò.
ÆäʵÕâÖÖÇé¿ö¾³£Óöµ½.
ÏÂÃæ¸ø³öÔ´´úÂë:
1:Person.JavaÎļþ:-------------------------------
public class Person{
String name;
int age;
public Person(String name,int age){ ......
ÔçÉÏÆð´²ÓеãÍíÁË£¬µ½Ñ§Ð£µÃʱºò¶¼8£º50ÁË£¬Ò»½ø½ÌÊÒ¿´¼ûÕÅТÏéÀÏʦÔÚ½ÌÊÒÀï¸øͬѧÃǽâ¾öÎÊÌ⣬²»¼ûÁËÀϱϵÄÉíÓ°£¬»¹ÒÔΪ×ß´í½ÌÊÒÁËÄØ£¬°´Àí˵Õâ¸öʱºòÀϱÏÓ¦¸ÃÔçÀ´Á˲ÅÊÇ£¬Ò»ÎʲÅÖªµÀÊDZÏÀÏʦ²¡ÁË,È¥Ò½Ôº¼ì²éÈ¥ÁË£¬Ï£Íû±ÏÀÏʦÄÜÔçµãºÃÆðÀ´£¡
½ñÌìÊÇÕÅÀÏʦ¸øÎÒÃÇÉϿΣ¬ÉîÈëÆÊÎöservlet£¬ÔÚûÀ´´«ÖDz¥¿Í֮ǰ¿´¹ý²»ÉÙÒÔÇ°ÕÅÀ ......
1¡¢Oracle8/8i/9iÊý¾Ý¿â£¨thinģʽ£©
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orclΪÊý¾Ý¿âµÄSID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);
2¡¢DB ......