JAVA²¢·¢±à³Ì±Ê¼Ç£¨3£©
Publication and Escape ·¢²¼Ò»¸ö¶ÔÏóÒâζ×ÅÄܹ»´ÓËüÄܹ»´ÓËüµ±Ç°·¶Î§Ö®Íâ±»·ÃÎʵ½¡£±ÈÈ磬±£´æ¿ÉÒÔ±»ÆäËû´úÂëÕÒµ½µÄÕâ¸ö¶ÔÏóµÄÒ»¸öÒýÓ㬻òÕß½«Æä×÷Ϊһ¸ö·Ç˽Óз½·¨µÄ·µ»ØÖµ£¬»òÕß½«Æä×÷Ϊ²ÎÊý´«µÝ¸øÆäËüµÄÀà¡£ public static Set<Secret> knownSecrets;
public void initialize() {
knownSecrets = new HashSet<Secret>();
}
Õâ¸öknownSecrets¾Í±»·¢²¼ÁË£¬²¢ÇÒ£¬knownSecretsÀïÃæÌí¼ÓµÄËùÓÐsecretÒ²¶¼±»·¢²¼³öÈ¥ÁË¡£
class UnsafeStates {
private String[] states = new String[] {
"AK", "AL" ...
};
public String[] getStates() { return states; }
}
Õâ¸östates¾Í±»·¢²¼ÁË£¬¶øÇÒ´øÀ´µÄÎÊÌâÊÇÈκε÷ÓÃÕ߶¼Äܹ»ÐÞ¸ÄÀïÃæµÄÄÚÈÝ£¬ÕâÑù£¬ºÜ²»°²È«¡£
public class ThisEscape {
public ThisEscape(EventSource source) {
source.registerListener(
new EventListener() {
public void onEvent(Event e) {
doSomething(e);
}
});
}
}
¿´Ã÷°×Õâ¸öThisEscapeΪʲô±»·¢²¼ÁËô£¿ÒòΪËü·¢²¼ÁËEventListener£¬¶øÄÚ²¿Àà»á´øÓÐÒ»¸öÒþʽµÄ¶ÔÍⲿÀàµÄÒýÓá£
ÁíÍ⣬ÉÏÃæÕâ¸öÀàµÄÒ»¸öÑÏÖØÎÊÌâÊÇ£¬ÔÚ¹¹Ôì·½·¨ÖÐthisÒýÓÃescapeÁË¡£ÕâÒ²¾ÍÒâζ×ÅÕâ¸ö¶ÔÏóÔÚ±»·¢²¼µÄʱºò»¹´¦ÓÚÒ»¸ö²»ÍêÕûµÄ״̬¡£Ò»¸ö³£¼ûµÄÇéÐξÍÊÇÔÚÀàµÄ¹¹Ôì·½·¨ÖÐÆô¶¯Ò»¸öÏ̡߳£When an object creates a thread from its constructor, it almost always shares its this reference with the new thread, either explicitly (by passing it to the constructor) or implicitly (because the Thread or Runnable is an inner class of the owning object). µ±È»Õâ±¾ÉíûʲôÎÊÌ⣬µ«×îºÃ²»ÒªÔÚ¹¹Ôì·½·¨ÖоÍÆô¶¯Õâ¸öỊ̈߳¬¶øÊÇ°ÑThread.start()·Åµ½Ò»¸öµ¥¶ÀµÄinit·½·¨Öй©ÆäËü¶ÔÏóµ÷Óá£
Òª±ÜÃâ²»ÍêÈ«¹¹ÔìµÄÎÊÌ⣬¿ÉÒÔ²ÉÓÃÏÂÃæµÄ·½Ê½£º
public class SafeListener {
private final EventListener listener;
private SafeListener() {
listener = new EventListener() {
public void onEvent(Event e) {
doSomething(e);
}
};
}
public static SafeListener newInstance(EventSource source) {
SafeListener safe =
Ïà¹ØÎĵµ£º
FlexÔÚºÍjavaµ÷Óùý³ÌÖУ¬ÆäʵÓиü¼ÓÁé»îµÄ°ì·¨À´Ìá¸ßϵͳµÄÃô½Ý¶È£¬¼«´óÌá¸ßϵͳµÄñîºÏºÍ¿ª·¢Ð§ÂÊ¡£ÔÚÕâÀïÎÒ¼ÌÐø¸ø´ó¼Ò½éÉÜһϣ¬Ãô½Ýµ÷ÓÃÖеÄjava¶¯Ì¬´úÂëµ÷Óá£
ÏÈ¿´javaµÄÀà(BeanShell)ÕâÀïÖ÷ÒªÊǽÓÊÕjava´úÂëÀ´¶¯Ì¬µ÷ÓõÄ
package com.shine.framework.be ......
¡¡¡¡¾Ý¹úÍâýÌ屨µÀ£¬MozillaÒѾÕë¶ÔFirefoxÉèÖÃÁËÒ»¸ö¶ñÒâJava²å¼þÆÁ±ÎºÚÃûµ¥¡£
¡¡¡¡¾Ý°²È«²©¿ÍBrian Krebs½éÉÜ£¬ÉÏÖܼ׹ÇÎĹ«Ë¾·¢²¼ÁËÒ»¸öÐÞ¸´Java²å¼þ©¶´µÄ¸üУ¬µ«ÊÇMozilla¿ª·¢ÈËÔ±·¢ÏÖ£¬Õâ¸ö¸üÐÂûÓÐɾ³ý¾É°æ±¾µÄ´úÂ룬ʹµÃϵͳÈÔÈ»ÁôÏÂÁË©¶´´úÂ룬´Ó¶øʹµÃÒÅÁô´úÂë³ÉΪеĩ¶´¡£¶Ô´Ë£¬Mozilla²»µÃ²»½«ÆÁ± ......
Ò»¡¢ÏòÉÏתÐÍ¡£
ͨË׵ؽ²¼´Êǽ«×ÓÀà¶ÔÏóתΪ¸¸Àà¶ÔÏó¡£´Ë´¦¸¸Àà¶ÔÏó¿ÉÒÔÊǽӿڡ£
1£¬ÏòÉÏתÐÍÖеķ½·¨µ÷Óá£
¿´ÏÂÃæ´úÂ룺
package com.wensefu.others;
public class Animal {
public void eat(){
System.out.println("animal eatting...");
}
}
class Bird extends Animal{
public void ......
ϵͳ×Ô¶¯Å׳öµÄÒì³£
ËùÓÐϵͳ¶¨ÒåµÄ±àÒëºÍÔËÐÐÒì³£¶¼¿ÉÒÔÓÉϵͳ×Ô¶¯Å׳ö£¬³ÆΪ±ê×¼Òì³££¬²¢ÇÒ Java Ç¿ÁÒµØÒªÇóÓ¦ÓóÌÐò½øÐÐÍêÕûµÄÒì³£´¦Àí£¬¸øÓû§ÓѺõÄÌáʾ£¬»òÕßÐÞÕýºóʹ³ÌÐò¼ÌÐøÖ´ÐС£
Óï¾äÅ׳öµÄÒì³£
Óû§³ÌÐò×Ô¶¨ÒåµÄÒì³£ºÍÓ¦ÓóÌÐòÌض¨µÄÒì³£,±ØÐë½èÖúÓÚ throws ºÍ throw Óï¾äÀ´¶¨ÒåÅ׳öÒì³£¡£
throwÊÇÓï¾äÅ׳ö ......
¹ØÓÚvolatile
ÎÒÃÇÖªµÀ£¬ÔÚJavaÖÐÉèÖñäÁ¿ÖµµÄ²Ù×÷£¬³ýÁËlongºÍdoubleÀàÐ͵ıäÁ¿ÍⶼÊÇÔ×Ó²Ù×÷£¬Ò²¾ÍÊÇ˵£¬¶ÔÓÚ±äÁ¿ÖµµÄ¼òµ¥¶Áд²Ù×÷ûÓбØÒª½øÐÐͬ²½¡£
ÕâÔÚJVM 1.2֮ǰ£¬JavaµÄÄÚ´æÄ£ÐÍʵÏÖ×ÜÊÇ´ÓÖ÷´æ¶ÁÈ¡±äÁ¿£¬ÊDz»ÐèÒª½øÐÐÌرðµÄ×¢ÒâµÄ¡£¶øËæ×ÅJVMµÄ³ÉÊìºÍÓÅ»¯£¬ÏÖÔÚÔÚ¶àÏ̻߳·¾³ÏÂ
volatile¹Ø¼ü×ÖµÄʹÓñäµÃ·Ç³£ÖØ ......