JavaÏß³Ìͬ²½ £¨synchronized wait notify£©
×¢£ºwait notify ¶¼ÊÇObjectµÄ·½·¨
ͬ²½£¨×èÈû£© £ºÊÇÒ»ÖÖ·ÀÖ¹¶Ô¹²Ïí×ÊÔ´·ÃÎʵ¼ÖµÄÊý¾Ý²»Ò»ÖµÄÒ»ÖÖģʽ¡£
ÏêϸÇë²Î¿´²Ù×÷ϵͳ¡£
ÔÚJavaÖУ¬ÓÉÓÚ¶Ô¶àÏ̵߳ÄÖ§³Ö£¬¶Ôͬ²½µÄ¿ØÖÆÖ÷Ҫͨ¹ýÒÔϼ¸¸ö·½·¨£¬synchronized£¬ºÍwait(),notify()ºÍnotifyAll(),ÏÂÃæ½øÐÐÒ»Ò»µÄ½²½â£º
A¹Ø¼ü×Ösynchronized
ÿ¸öjava¶ÔÏó¶¼ÓÐÒ»°ÑËø£¬ µ±Óжà¸öÏß³Ìͬʱ·ÃÎʹ²Ïí×ÊÔ´µÄʱºò£¬ ÐèÒªSynchronize À´¿ØÖÆ°²È«ÐÔ£¬ synchronize ·Ö synchronize ·½·¨ ºÍsynchronize¿é£¬Ê¹ÓÃsynchronize¿éʱ£¬ Ò»¶¨ÒªÏÔʾµÄ»ñµÃ¸Ã¶ÔÏóµÄËø£¨Èçsynchronize£¨object))¶ø·½·¨Ôò²»ÐèÒª¡£
javaµÄÄÚ´æÄ£ÐÍÊǶÔÿһ¸ö½ø³ÌÓÐÒ»¸öÖ÷Äڴ棬 ÿ¸öÏß³ÌÓÐ×Ô¼ºµÄÄڴ棬 ËûÃÇ´ÓÖ÷ÄÚ´æÖÐÈ¡Êý¾Ý£¬ È»ºó¼ÆË㣬 ÔÙ´æÈëÖ÷ÄÚ´æÖС£
²¢·¢ÎÊÌâÈçÏ£ºÈç¹û¶à¸öÏß³ÌͬʲÙ×÷ͬһÊý¾Ý£¬ AÏ̴߳ÓÖ÷ÄÚ´æÖÐÈ¡µÄIµÄֵΪ1£¬ È»ºó½øÐмÓ1²Ù×÷£¬ ÕâʱBÏß³ÌҲȡIµÄÖµ£¬ ½øÐмÓ2²Ù×÷£¬ È»ºóA´æÈë2µ½Ö÷ÄÚ´æÖУ¬ BÒ²´æÈ룬 ÕâÑù¾Í¸²¸ÇÁËAµÄÖµ£¨Í¬Êý¾Ý¿âÖеIJ¢·¢ÎÊÌâÒ»Ñù£©¡£
½â¾ö°ì·¨ÊÇÓÃsynchronize£¬ ÈçÓÃsynchronized£¨I£©¡£±»synchronize ÐÞÊεķ½·¨£¨¿é£©°ÑÒÔÏÂÈý²½²Ù×÷µ±³ÉÒ»¸öÔ×Ó²Ù×÷£ºÈ¡Êý¾Ý£¬ ²Ù×÷Êý¾Ý£¬ ´æÊý¾Ý¡£ ÎÒÃÇÖªµÀÔ×Ó²Ù×÷ÊDz»¿ÉÒÔ±»´ò¶ÏµÄ£¬ ËùÒÔÆä±£Ö¤ÁËÊý¾ÝÒ»ÖÂÐÔ£¬ ÕâÑùͬһʱ¼äÖ»ÓÐÒ»¸öÏß³ÌÔÙÖ´ÐУ¬ ¶ÔÐÔÄÜÓÐÒ»¶¨µÄÓ°Ïì¡£ÕâÒ²ÊÇsynchronizeµÄµÚ¶þ¸ö×÷Ó㺱£Ö¤Í³Ò»Ê±¼äÖ»ÓÐÒ»¸öÏß³ÌÔÙÔËÐС£ µ±ÊµÏÖSOCKETÁ¬½ÓµÄʱºò¾³£Óõ½.
JAVAÖй涨¶Ô·ÇFLOAT, LONGµÄÔʼÀàÐ͵ÄÈ¡ºÍ´æ²Ù×÷ΪÔ×Ó²Ù×÷¡£ Æäʵ¾ÍÊǶÔÒ»¸ö×Ö£¨32룩µÄÈ¡£¬´æλÔʼ²Ù×÷£¬ ÒòΪFLOAT, LONGΪÁ½¸ö×ֽڵij¤¶È£¬ ËùÒÔÆäÈ¡£¬ ´æΪ·ÇÔ×Ó²Ù×÷¡£ Èç¹ûÏë°ÑËûÃÇÒ²±äΪÔ×Ó²Ù×÷£¬ ¿ÉÒÔÓÃVOLATILE¹Ø¼ü×ÖÀ´ÐÞÊÎ
ʹÓ÷½·¨£º
×÷ÓÃÇøÓòÖ÷ÒªÓÐÁ½ÖÖ£º
1.·½·¨
2.´úÂë¿é
±»synchronizedÉùÃ÷µÄ·½·¨±»³ÆΪͬ²½·½·¨£¬±»ÆäÐÞÊεĴúÂë¿é³ÆΪͬ²½Óï¾ä¡£ÎÞÂÛÊÇͬ²½·½·¨»¹ÊÇͬ²½Óï¾ä£¬Ö»ÒªÉùÃ÷Ϊͬ²½ÁË£¬ÔÚͬһʱ¿Ì£¬Í¬Ò»¸ö¶ÔÏóµÄͬ²½XXÊDz»¿ÉÒÔ±»Í¬Ê±·ÃÎʵģ¬¶ø²»Í¬¶ÔÏóÖ®¼äµÄͬ²½·½·¨ÊÇ»¥²»¸ÉÈŵġ£
¾ßÌåʵÏÖ£¨ÈçÏ´úÂ붼ÔÚij¸öÀඨÒåÖУ©£º
ͬ²½·½·¨£º
Public synchronized void change() {
//
}
ͬ²½Óï¾ä
Ïà¹ØÎĵµ£º
1.×î¼òµ¥µÄͼÐΣ¬Ò»¸öÏûÏ¢¿ò
import javax.swing.JOptionPane;
//±íÃ÷³ÌÐòʹÓÃjavax.swing°üµÄJOptionPaneÀà
public class Dialog1{
public static void main(String[] args) {
// TODO Auto-generated method stub
&n ......
½ñÌìÅöµ½Ò»¸öºÜÓÐÒâ˼µÄÎÊÌ⣬¾ÍÊǹØÓÚʹÓÃLinkedList×÷ΪHashMap»òÕßHashtableµÃkey£¬µ«ÊÇ×îºó·¢ÏÖÊý¾Ý²¢Ã»Óдæ½øÈ¥¡£
Ê×ÏÈ˵һÏÂHashMap£¬Hashtable°É£¬ËüÃǶ¼¼Ì³ÐÁËCloneable, Map, Serializable¡£ËüÃÇÁ½¸ö»ù±¾ÉÏÊÇÒ»ÑùµÄ£¬“The HashMap
class is roughly equivalent to Hashtable
, except that it is
unsy ......
1, ±£Ö¤Ḭ̈߳²È«µÄÈýÖÖ·½·¨:
a, ²»Òª¿çÏ̷߳ÃÎʹ²Ïí±äÁ¿
b, ʹ¹²Ïí±äÁ¿ÊÇfinalÀàÐ͵Ä
c, ½«¹²Ïí±äÁ¿µÄ²Ù×÷¼ÓÉÏͬ²½
2, Ò»¿ªÊ¼¾Í½«ÀàÉè¼Æ³ÉḬ̈߳²È«µÄ, ±ÈÔÚºóÆÚÖØÐÂÐÞ¸´Ëü,¸üÈÝÒ×.
3, ±àд¶àÏ̳߳ÌÐò, Ê×Ïȱ£Ö¤Ë ......
Map map = new HashMap();
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
}
......