java¶àÏß³ÌʹÓÃSynchronized¹Ø¼ü×Öͬ²½Àà·½
ÒªÏë½â¾ö“ÔàÊý¾Ý”µÄÎÊÌ⣬×î¼òµ¥µÄ·½·¨¾ÍÊÇʹÓÃsynchronized¹Ø¼ü×ÖÀ´Ê¹run·½·¨Í¬²½£¬´úÂëÈçÏ£º
public synchronized void run()
{
}
´ÓÉÏÃæµÄ´úÂë¿ÉÒÔ¿´³ö£¬Ö»ÒªÔÚvoidºÍpublicÖ®¼ä¼ÓÉÏsynchronized¹Ø¼ü×Ö£¬¾Í¿ÉÒÔʹrun·½·¨Í¬²½£¬Ò²¾ÍÊÇ˵£¬¶ÔÓÚͬһ¸öJavaÀàµÄ¶ÔÏóʵÀý£¬run·½·¨Í¬Ê±Ö»Äܱ»Ò»¸öÏ̵߳÷Ó㬲¢µ±Ç°µÄrunÖ´ÐÐÍêºó£¬²ÅÄܱ»ÆäËûµÄÏ̵߳÷Ó᣼´Ê¹µ±Ç°Ïß³ÌÖ´Ðе½ÁËrun·½·¨ÖеÄyield·½·¨£¬Ò²Ö»ÊÇÔÝÍ£ÁËһϡ£ÓÉÓÚÆäËûÏß³ÌÎÞ·¨Ö´ÐÐrun·½·¨£¬Òò´Ë£¬×îÖÕ»¹ÊÇ»áÓɵ±Ç°µÄÏß³ÌÀ´¼ÌÐøÖ´ÐС£ÏÈ¿´¿´ÏÂÃæµÄ´úÂ룺
sychronized¹Ø¼ü×ÖÖ»ºÍÒ»¸ö¶ÔÏóʵÀý°ó¶¨
class Test
{
public synchronized void method()
{
}
}
public class Sync implements Runnable
{
private Test test;
public void run()
{
test.method();
}
public Sync(Test test)
{
this.test = test;
}
public static void main(String[] args) throws Exception
{
Test test1 = new Test();
Test test2 = new Test();
Sync sync1 = new Sync(test1);
Sync sync2 = new Sync(test2);
new Thread(sync1).start();
new Thread(sync2).start();
}
}
ÔÚTestÀàÖеÄmethod·½·¨ÊÇͬ²½µÄ¡£µ«ÉÏÃæµÄ´úÂ뽨Á¢ÁËÁ½¸öTestÀàµÄʵÀý£¬Òò´Ë£¬test1ºÍtest2µÄmethod·½·¨ÊÇ·Ö±ðÖ´Ðеġ£ÒªÏëÈÃmethodͬ²½£¬±ØÐëÔÚ½¨Á¢SyncÀàµÄʵÀýʱÏòËüµÄ¹¹Ôì·½·¨Öд«Èëͬһ¸öTestÀàµÄʵÀý£¬ÈçÏÂÃæµÄ´úÂëËùʾ£º
Sync sync1 = new Sync(test1);
²»½ö¿ÉÒÔʹÓÃsynchronizedÀ´Í¬²½·Ç¾²Ì¬·½·¨£¬Ò²¿ÉÒÔʹÓÃsynchronizedÀ´Í¬²½¾²Ì¬·½·¨¡£Èç¿ÉÒÔ°´ÈçÏ·½Ê½À´¶¨Òåmethod·½·¨£º
class Test
{
public static synchronized void method() { }
}
½¨Á¢TestÀàµÄ¶ÔÏóʵÀýÈçÏ£º
Test test = new Test();
¶ÔÓÚ¾²Ì¬·½·¨À´Ëµ£¬Ö»Òª¼ÓÉÏÁËsynchronized¹Ø¼ü×Ö£¬Õâ¸ö·½·¨¾ÍÊÇͬ²½µÄ£¬ÎÞÂÛÊÇʹÓÃtest.method()£¬»¹ÊÇʹÓÃTest.method()À´µ÷ÓÃmethod·½·¨£¬method¶¼ÊÇͬ²
Ïà¹ØÎĵµ£º
µ±ÖØÔØequals·½·¨Ê±,×ñѹßÀý.
equals²»ÊÇÿ¸öÀ඼±ØÐëÖØÔØ,µ±ÒÔϼ¸¸öÌõ¼þÂú×ã,¿ÉÒÔ²»ÖØÔØequals.
1. ÀàµÄʵÀýÖ»ÓÐͬһ¸ö²Å±»ÅÐΪÏàµÈ. ÈçThreadÀà.
2. ²»ÓùØÐÄÁ½¸ö¶ÔÏóÊÇ·ñÂß¼ÏàµÈµÄʱºò,»òÊÇ˵±È½Ï¶ÔÏóûÓÐÒâÒå.
3.¸¸ÀàÒѾÖØÔØÁËequals,¶øÇÒ¸¸ÀàµÄequalsÒ²ÊÊÓÃÓÚ×ÓÀà,×ÓÀà¾Í²»ÓÃÖØÔØequals.
4.ÕâÊÇÒ»¸ö˽ÓÐÀà»òÊ ......
javaÖеÄËÄÖÖÀàÐ͵ļ¯ºÏÀà
£¨1£©Vector £¨2£©BitSet£¨3£©Stack£¨4£©HashTable
2ö¾ÙÆ÷
ÔÚÈκμ¯ºÏÀàÖУ¬±ØÐëͨ¹ýijÖÖ·½·¨ÔÚÆäÖÐÖÃÈë¶ÔÏó£¬ÔÙÓÃÁíÒ»ÖÖ·½·¨´ÓÖÐÈ¡µÃ¶ÔÏó¡£ÔÚVectorÖУ¬addElement()¾ÍÊÇÎÒÃDzåÈë¶ÔÏó²ÉÓõķ½·¨
£¬¶þelementAt()ÔòÊÇÌáÈ¡¶ÔÏóµÄΨһ·½·¨¡£
Iterator¿ÉÒÔ´ïµ½Õâ¸öÄ¿µÄ£¬ËüÊÇÒ»¸ö¶ÔÏó£¬×÷ÓÃÊDZ ......
JavaÃæÏò¶ÔÏó±à³Ì-6.3 ·½·¨¸²¸Ç£¨Override£©¼ÙÈçÓÐ100¸öÀ࣬·Ö±ðΪSub1£¬Sub2…Sub100£¬ËüÃǵÄÒ»¸ö¹²Í¬ÐÐΪÊÇд×Ö£¬³ýÁËSub1ÀàÓýÅд×ÖÍ⣬ÆäÓàµÄÀ඼ÓÃÊÖд×Ö¡£¿ÉÒÔ³éÏó³öÒ»¸ö¸¸ÀàBase£¬ËüÓÐÒ»¸ö±íʾд×ֵķ½·¨write()£¬ÄÇôÕâ¸ö·½·¨µ½µ×ÈçºÎʵÏÖÄØ£¿´Ó¾¡¿ÉÄÜÌá¸ß´úÂë¿ÉÖØÓÃÐԵĽǶȿ´£¬write()·½·¨Ó¦¸Ã²ÉÓÃÊÊÓÃÓÚ´ó ......
Java»ù´¡Ð¡½á(ÊýÖµÀàÐÍת»»¹æÔò)
Ò»¡¡ÊýÖµÀàÐÍת»»¹æÔò
ÊýÖµÐÍÊý¾Ý½øÐÐÔËËãʱ£¬Èç¹û²Ù×÷ÊýµÄÀàÐͲ»Í¬»ò²Ù×÷ÊýµÄÀàÐ;ùµÍÓÚintÐÍ£¬Ôò
»á½øÐÐ×Ô¶¯ÀàÐÍת»»´¦Àí£¬¾ßÌå¹æÔòÈçÏ£º
1)Èç¹ûÁ½¸ö²Ù×÷ÊýÖÐÓÐÒ»¸öÊÇdoubleÐÍ£¬ÔòϵͳÏȽ«ÁíÒ»¸ö²Ù×÷ÊýµÄֵת»»Îªdouble
ÐÍ£¬È»ºóÔÙ½øÐÐÔËË㣬·ñÔò
2)Èç¹ûÁ½¸ö²Ù×÷ÊýÖÐÓÐÒ»¸öÊÇfloat ......
Java¶ÔÏóÀàÐÍת»»Ê±Java¿ª·¢Öо³£Óöµ½µÄ£¬±¾ÎÄÏòÄú½éÉÜJava¶ÔÏóÀàÐÍת»»µÄһЩ¼¼ÇɺÍ×¢ÒâÊÂÏ°üÀ¨ÏòÉÏתÐÍÓëÏòÏÂתÐͶ¼²Ù×÷ºÍÖ÷Òªµã¡£
Ò»¡¢ÏòÉÏתÐÍÓëÏòÏÂתÐÍ¡£
¶ÔÏóÀàÐ͵Äת»»ÔÚJavaÓïÑÔƽ̨Öо³£Óöµ½£¬Ö÷Òª°üÀ¨ÏòÉÏתÐÍÓëÏòÏÂתÐͲÙ×÷¡£³ÌÐò¿ª·¢ÈËÔ±ÐèÒªÊìÁ· ......