java²¢·¢±à³Ìʵ¼ù ±Ê¼Ç£¨1£©
Ḭ̈߳²È« ʲôÊÇḬ̈߳²È«(thread-safe)£¿ Ò»¸öÀ࣬Èç¹ûÄܹ»ÔÚ¶àÏ̲߳¢·¢·ÃÎʵĻ·¾³Ï£¨²»¹ÜÏ̷߳ÃÎʵÄ˳Ðò£©ÕýÈ·±íÏÖ×Ô¼ºµÄÐÐΪ£¬²¢ÇÒÎÞÐèÍⲿµ÷ÓÃÌí¼ÓÈκÎͬ²½Ö®ÀàµÄ²Ù×÷£¬Õâ¸öÀà¾ÍÊÇḬ̈߳²È«µÄ¡£
Õâ¸öÕýÈ·ÐÔ¿ÉÒÔÕâôÀí½â£¬¾ÍÊÇ˵¶àÏ̷߳ÃÎʵĽá¹û²»»á²»Í¬ÓÚµ¥Ï̵߳ķÃÎÊ¡£
Ḭ̈߳²È«µÄÀ಻ÐèÒªÍⲿµ÷ÓÃÌṩÈκθ½¼ÓµÄͬ²½¡£ ÎÞ״̬(stateless)µÄÀàÓÀÔ¶ÊÇḬ̈߳²È«µÄ¡£
ʲôÊÇÎÞ״̬µÄÀࣿûÓÐʵÀý±äÁ¿(field)£¬Ò²Ã»ÓÐÒýÓÃÆäËüÀàµÄfield¡£ Ô×ÓÐÔ A race condition occurs when the correctness of a computation depends on the relative timing or interleaving of multiple threads by the runtime; in other words, when getting the right answer relies on lucky timing. ×î³£¼ûµÄrace conditionÊÇcheck-and-act¡£Òª·ÀÖ¹ÕâÖÖrace condition£¬ÎÒÃÇÐèÒªÔ×ÓÐԵIJÙ×÷¡£Ê²Ã´ÊÇÔ×ÓÐÔÄØ£¬¾ÍÊÇ˵£¬ÄãÕâ¸ö²Ù×÷ÔÚÖ´ÐеĹý³ÌÖУ¬¶ÔÓÚÄã²Ù×÷µÄ״̬ÉÏÆäËüµÄ²Ù×÷£¨°üÀ¨Äã×Ô¼º£©ÒªÃ´È«¶¼Ö´ÐÐÍêÁË£¬ÒªÃ´»¹Ã»¿ªÊ¼¡£
Ïë˵¾äͨ˳µÄÖйú»°ÔõôÕâôÄѰ¡£¬»¹ÊǸø³öÔÎİɣº Operations A and B are atomic with respect to each other if, from the perspective of a thread executing A, when another thread executes B, either all of B has executed or none of it has. An atomic operation is one that is atomic with respect to all operations, including itself, that operate on the same state. À´¸öÀý×Ó @NotThreadSafe
public class UnsafeCountingFactorizer implements Servlet {
private long count = 0;
public long getCount() { return count; }
public void service(ServletRequest req, ServletResponse resp) {
BigInteger i = extractfromRequest(req);
BigInteger[] factors = factor(i);
++count;
encodeIntoResponse(resp, factors);
}
}
@ThreadSafe
public class CountingFactorizer implements Servlet {
private final AtomicLong count = new AtomicLong(0);
public long getCount() { return count.get(); }
public void service(ServletRequest req, ServletResponse resp) {
BigInteger i = extractfromRequest(req);
BigInteger[] factors = factor(i);
count.incrementAndGet();
Ïà¹ØÎĵµ£º
ÔÚ´«ÖǵÄÕâ¶Îʱ¼äÿÌìµÄÇéÐÎÏë¶¼ÏëµÃµ½£¬Á½µãÒ»Ïߣ¬µ«ÊÇÿÌì¶¼¸Ð¾õµ½ºÜ³äʵ£¬Ñ§Ï°£¬²»Í£µÄѧϰ¡£Ô½ÊDz»Í£µÄѧϰ£¬Ô½ÊǸоõ×Ô¼ºµÄÁ¦Á¿ÊÇÄÇôµÄ΢±¡£¬×Ô¼ºµÄÄÜÁ¦ÊÇÄÇôµÄС£¬JAVAÕæÊDz©´ó¾«Éî¡£ÀÏʦ˵½ÐÎÒÃDz»Òªµ£ÐÄ£¬¿ÉÊÇÎÒ²»µ£ÐÄÒ²²»¿ÉÄÜŶ£¬±Ï¾¹ÔÚͬÀàѧУ±ÏÒµµÄ£¬Ò²ÐíÎÒÃÇ¿ÉÒÔËãµÃÉÏÊǺܲ»´íµÄ£¬µ«ÊǺÍÎÒÃÇ̨ͬ¾º¼¼µÄÈË ......
JavaʹµÃ¸´ÔÓÓ¦ÓõĿª·¢±äµÃÏà¶Ô¼òµ¥£¬ºÁÎÞÒÉÎÊ£¬ËüµÄÕâÖÖÒ×ÓÃÐÔ¶ÔJavaµÄ´ó·¶Î§Á÷Ðй¦²»¿Éû¡£È»¶ø£¬ÕâÖÖÒ×ÓÃÐÔʵ¼ÊÉÏÊÇÒ»°ÑË«Èн£¡£Ò»¸öÉè¼ÆÁ¼ºÃµÄJava³ÌÐò£¬ÐÔÄܱíÏÖÍùÍù²»ÈçÒ»¸öͬÑùÉè¼ÆÁ¼ºÃµÄC++³ÌÐò¡£ÔÚJava³ÌÐòÖУ¬ÐÔÄÜÎÊÌâµÄ´ó²¿·ÖÔÒò²¢²»ÔÚÓÚJavaÓïÑÔ£¬¶øÊÇÔÚÓÚ³ÌÐò±¾Éí¡£Ñø³ÉºÃµÄ´úÂë±àдϰ¹ß·Ç³£ÖØÒª£¬±ÈÈçÕýÈ·µ ......
Ñ¡ÔñµÄ·¶Î§Ì«¹ã£¬¿ÉÒÔ¶ÁµÄÊéÌ«¶à£¬ÍùÍùÈÝÒ×ÎÞËùÊÊ´Ó¡£ÎÒÏë¾ÍÎÒ×Ô¼º¶Á¹ýµÄ¼¼ÊõÊé¼®ÖÐÌôÑ¡³öÀ´Ò»Ð©£¬°´ÕÕѧϰµÄÏȺó˳Ðò£¬ÍƼö¸ø´ó¼Ò£¬ÌرðÊÇÄÇЩÏë²»¶ÏÌá¸ß×Ô¼º¼¼ÊõˮƽµÄJava³ÌÐòÔ±ÃÇ¡£
Ò»¡¢Java±à³ÌÈëÃÅÀà
¶ÔÓÚûÓÐJava±à³Ì¾ÑéµÄ³ÌÐòÔ±ÒªÈëÃÅ£¬Ëæ±ã¶ÁʲôÈëÃÅÊé¼®¶¼Ò»Ñù£¬Õâ¸ö½×¶ÎÐèÒªÄã¿ìËÙµÄÕÆÎÕJava»ù´¡Óï·¨ºÍ»ù±¾Óà ......
1¡¢´´½¨ÁËÒ»¸ö¶ÔÏóºó£º
£¨1£©Ã»ÓÐÔÚÊʵ±µÄµØ·½Êͷŵô
£¨2£©ÔÚÓ¦¸ÃÊͷŵĵط½Ã»ÓÐ×öÊͷŲÙ×÷
ÀýÈ磺ÏÂÃæÒ»¶Î³ÌÐò£º
m_progressDlg = ProgressDialog.show(this, getString(R.string.market),getString(R.string.is_visiting), true);
new Thread() {
public v ......
URI ÊÇ×ÊÔ´±êʶ·û¡£¾ÍÊÇÏ൱ÓÚÒ»¸öÈ˵ļÒͥסַ¡£
URLºÍURIÀàËÆ¡£ÊÇ×ÊÔ´¶¨Î»µÄ¡£ ºÍURI²»Í¬µÄ¾ÍÊÇURLÌṩÁË»ñÈ¡¶«Î÷µÄ·½·¨¡£
java.io.InputStream l_urlStream;
// Ò²¿ÉÒÔ»»³Éuri,È»ºóµ÷ÓÃuri.toURL
  ......