Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

Javaͬ²½»úÖÆ£ºsychronized¶Ô´úÂëÓ°Ïì

ת £ºhttp://kuru.javaeye.com/blog/288467
Java¶Ô¶àÏ̵߳ÄÖ§³ÖÓëͬ²½»úÖÆÉîÊÜ´ó¼ÒµÄϲ°®£¬Ëƺõ¿´ÆðÀ´Ê¹ÓÃÁËsynchronized¹Ø¼ü×־ͿÉÒÔÇáËɵؽâ¾ö¶àÏ̹߳²ÏíÊý¾Ýͬ²½ÎÊÌâ¡£µ½µ×ÈçºÎ£¿¨D¨D»¹µÃ¶Ôsynchronized¹Ø¼ü×ÖµÄ×÷ÓýøÐÐÉîÈëÁ˽â²Å¿É¶¨ÂÛ¡£ ×ܵÄ˵À´£¬synchronized¹Ø¼ü×Ö¿ÉÒÔ×÷Ϊº¯ÊýµÄÐÞÊηû£¬Ò²¿É×÷Ϊº¯ÊýÄÚµÄÓï¾ä£¬Ò²¾ÍÊÇƽʱ˵µÄͬ²½·½·¨ºÍͬ²½Óï¾ä¿é¡£Èç¹ûÔÙϸµÄ·ÖÀ࣬synchronized¿É×÷ÓÃÓÚinstance±äÁ¿¡¢object reference£¨¶ÔÏóÒýÓã©¡¢staticº¯ÊýºÍclass literals(ÀàÃû³Æ×ÖÃæ³£Á¿)ÉíÉÏ¡£ ÔÚ½øÒ»²½²ûÊö֮ǰ£¬ÎÒÃÇÐèÒªÃ÷È·¼¸µã£º A£®ÎÞÂÛsynchronized¹Ø¼ü×Ö¼ÓÔÚ·½·¨ÉÏ»¹ÊǶÔÏóÉÏ£¬ËüÈ¡µÃµÄËø¶¼ÊǶÔÏ󣬶ø²»ÊÇ°ÑÒ»¶Î´úÂë»òº¯Êýµ±×÷Ëø¨D¨D¶øÇÒͬ²½·½·¨ºÜ¿ÉÄÜ»¹»á±»ÆäËûÏ̵߳ĶÔÏó·ÃÎÊ¡£ B£®Ã¿¸ö¶ÔÏóÖ»ÓÐÒ»¸öËø£¨lock£©ÓëÖ®Ïà¹ØÁª¡£ C£®ÊµÏÖͬ²½ÊÇÒªºÜ´óµÄϵͳ¿ªÏú×÷Ϊ´ú¼ÛµÄ£¬ÉõÖÁ¿ÉÄÜÔì³ÉËÀËø£¬ËùÒÔ¾¡Á¿±ÜÃâÎÞνµÄͬ²½¿ØÖÆ¡£ ½Ó×ÅÀ´ÌÖÂÛsynchronizedÓõ½²»Í¬µØ·½¶Ô´úÂë²úÉúµÄÓ°Ï죺 ¼ÙÉèP1¡¢P2ÊÇͬһ¸öÀàµÄ²»Í¬¶ÔÏó£¬Õâ¸öÀàÖж¨ÒåÁËÒÔϼ¸ÖÖÇé¿öµÄͬ²½¿é»òͬ²½·½·¨£¬P1¡¢P2¾Í¶¼¿ÉÒÔµ÷ÓÃËüÃÇ¡£ 1£®°Ñsynchronizedµ±×÷º¯ÊýÐÞÊηûʱ£¬Ê¾Àý´úÂëÈçÏ£º Public synchronized void methodAAA() { //…. } ÕâÒ²¾ÍÊÇͬ²½·½·¨£¬ÄÇÕâʱsynchronizedËø¶¨µÄÊÇÄĸö¶ÔÏóÄØ£¿ËüËø¶¨µÄÊǵ÷ÓÃÕâ¸öͬ²½·½·¨¶ÔÏó¡£Ò²¾ÍÊÇ˵£¬µ±Ò»¸ö¶ÔÏóP1ÔÚ²»Í¬µÄÏß³ÌÖÐÖ´ÐÐÕâ¸öͬ²½·½·¨Ê±£¬ËüÃÇÖ®¼ä»áÐγɻ¥³â£¬´ïµ½Í¬²½µÄЧ¹û¡£µ«ÊÇÕâ¸ö¶ÔÏóËùÊôµÄClassËù²úÉúµÄÁíÒ»¶ÔÏóP2È´¿ÉÒÔÈÎÒâµ÷ÓÃÕâ¸ö±»¼ÓÁËsynchronized¹Ø¼ü×ֵķ½·¨¡£ ÉϱߵÄʾÀý´úÂëµÈͬÓÚÈçÏ´úÂ룺 public void methodAAA() { synchronized (this) // (1) { //….. } } (1)´¦µÄthisÖ¸µÄÊÇʲôÄØ£¿ËüÖ¸µÄ¾ÍÊǵ÷ÓÃÕâ¸ö·½·¨µÄ¶ÔÏó£¬ÈçP1¡£¿É¼ûͬ²½·½·¨ÊµÖÊÊǽ«synchronized×÷ÓÃÓÚobject reference¡£¨D¨DÄǸöÄõ½ÁËP1¶ÔÏóËøµÄỊ̈߳¬²Å¿ÉÒÔµ÷ÓÃP1µÄͬ²½·½·¨£¬¶ø¶ÔP2¶øÑÔ£¬P1Õâ¸öËøÓëËüºÁ²»Ïà¸É£¬³ÌÐòÒ²¿ÉÄÜÔÚÕâÖÖÇéÐÎÏ°ÚÍÑͬ²½»úÖƵĿØÖÆ£¬Ôì³ÉÊý¾Ý»ìÂÒ 2£®Í¬²½¿é£¬Ê¾Àý´úÂëÈçÏ£º public void method3(SomeObject so) { synchronized(so) { //….. } } Õâʱ£¬Ëø¾ÍÊÇsoÕâ¸ö¶ÔÏó£¬Ë­Äõ½Õâ¸öËøË­¾Í¿ÉÒÔÔËÐÐËüËù¿ØÖƵÄÄǶδúÂë¡£µ±ÓÐÒ»¸öÃ÷È·µÄ¶ÔÏó×÷ΪËøʱ£¬¾Í¿ÉÒÔÕâÑùд³ÌÐò£¬µ«µ±Ã»ÓÐÃ÷È·µÄ¶ÔÏó×÷ΪËø£¬Ö»ÊÇÏëÈÃÒ»¶Î´úÂëͬ²½Ê±£¬¿ÉÒÔ´´½¨Ò»¸öÌØÊâµÄinstance±äÁ¿£¨ËüµÃÊÇÒ»¸ö¶ÔÏó£©À´³äµ±Ëø£º class Foo


Ïà¹ØÎĵµ£º

ÓÃJavaдµÄ¼ÆËãÆ÷

                            ÓÃJavaдµÄ¼ÆËãÆ÷
package com.wenbo.myprogrem.day15;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.Actio ......

JAVA excel

<%    
 response.setContentType("application/vnd.ms-excel;charset=utf-8");
 String  reportname = (String)request.getAttribute("reportname");
 String  reportFileName=java.net.URLEncoder.encode(reportname, "UTF-8");
 String  filenamedispla ......

×ÔѧjavaÓÃÒ»ÄêµÄʱ¼äÄÜ·ñ³É¹¦£¿

ÎÒ´òËãÓýñÄêÒ»ÄêµÄʱ¼ä°Ñ´«ÖDz©¿ÍµÄjava¿Î³ÌѧÍê²»Öª¿É²»¿ÉÐУ¿ÔÚѧÁËËûÔÚÍøÉϵĿγÌÖ®ºó¶Ô×Ô¼ºµÄÏîĿʵսÊÇ·ñÓаïÖú£¿ËäÈ»»¹²»È·¶¨£¬µ«¾¡Á¦°É¡£·´Õý½ñÄêµÄʱ¼äÒ²²»¶à£¬»¹µÃÔÚÿÌìµÄÍíÉÏÏ°àʱ¼ä½øÐУ¬ºÜÐÁ¿àµÄ¡£Íâ¼ÓÉÏ×Ô¼ºÑÛ¸ßÊֵͣ¬Ö´Ðв»Á¦£¬Ð§¹ûºÜ¿ÉÄÜ´ó´óÕÛ¿Û¡£¾¡Á¦°É ......

java²¼¾Ö¹ÜÀíÆ÷

JAVAµÄ½çÃæ²¼¾ÖÔ­Àí£ºÒòΪJavaÊÇ¿çƽ̨ÓïÑÔ£¬Ê¹Óþø¶Ô×ø±êÏÔÈ»»áµ¼ÖÂÎÊÌ⣬¼´ÔÚ²»Í¬Æ½Ì¨¡¢²»Í¬·Ö±æÂÊϵÄÏÔʾЧ¹û²»Ò»Ñù¡£Java ΪÁËʵÏÖ¿çƽ̨µÄÌØÐÔ²¢ÇÒ»ñµÃ¶¯Ì¬µÄ²¼¾ÖЧ¹û£¬Java½«ÈÝÆ÷ÄÚµÄËùÓÐ×é¼þ°²ÅŸøÒ»¸ö“²¼¾Ö¹ÜÀíÆ÷”¸ºÔð¹ÜÀí£¬È磺ÅÅÁÐ˳Ðò£¬×é¼þµÄ´óС£¬Î»Öõȣ¬µ±´°¿ÚÒƶ¯»òµ÷Õû´óСºó×é¼þÈçºÎ±ä»¯µÈ¹ ......

JAVAÖеÄÒÀÀµµ¹Ô­Ôò

JAVAÖеÄÒÀÀµµ¹Ô­Ôò
ÃÀ¹ú·¨ÂÉÓÐÒ»Ìõ×î»ù±¾µÄ×¼Ôò¾ÍÊÇ“ÈËÈËƽµÈ”£¬ÎÒÃDz»È¥¹ÜËüÊǾ­¹ýʲôÑùµÄ¶·Õù¡¢Ê²Ã´ÑùµÄÁ÷ѪÎþÉü»»À´µÄ£¬Ö»°ÑËüÀí½âΪһ¸öJAVA·½·¨£¬¸Ã·½·¨¶¨ÒåÈçÏ£º
    public final boolean ÈËÈËƽµÈ(ÈË1£¬ÈË2){
       return true;
  &n ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ