How Java handles arguments of a class type.
=====suppose such a method:
public static void openFile(String fileName, PrintWriter stream) throws FileNotFoundException
{
stream = new PrintWriter(fileName);
}
=====then we want to use it this way:
PrintWriter toFile = null;
try
{
openFile("data.txt", toFile);
}
*****After this code is executed, the value of toFile is still null. The file that was opened in the method openFile went away when the method ended. The problem has to do with how Java handles arguments of a class type.
"These arguments are passed to the method as memory addresses that cannot be changed. The state of the object at the memory address normally can be changed, but the memory address itself cannot be changed"!
Ïà¹ØÎĵµ£º
1 Factory Pattern£¨¹¤³§Ä£Ê½£©
¡¡¡¡ÉϰñÀíÓÉ£º½«³ÌÐòÖд´½¨¶ÔÏóµÄ²Ù×÷£¬µ¥¶À³öÀ´´¦Àí£¬´ó´óÌá¸ßÁËϵͳÀ©Õ¹µÄÈáÐÔ£¬½Ó¿ÚµÄ³éÏ󻯴¦Àí¸øÏ໥ÒÀÀµµÄ¶ÔÏó´´½¨ÌṩÁË×îºÃµÄ³éÏóģʽ¡£
¡¡¡¡2 Facade Pattern
¡¡¡¡ÉϰñÀíÓÉ£º½«±íÏÖ²ãºÍÂß¼²ã¸ôÀ룬·â×°µ×²ãµÄ¸´ÔÓ´¦Àí£¬ÎªÓû§Ìṩ¼òµ¥µÄ½Ó¿Ú£¬ÕâÑùµÄÀý×ÓËæ´¦¿É¼û¡£ÃÅÃæÄ£Ê½ºÜ¶ ......
·Ç³£¼òµ¥,ÔÚC#ÖÐÖ»ÐèÒªÔÚÀàµÄ·½·¨µÄÉÏ·½ÇÃÈýÏÂ"/"¾Í×Ô¶¯°ïÄãÌí¼ÓÏà¹ØµÄ´úÂë,ÄãÖ»Òª°´×ÅÌîд¾Í¿ÉÒÔÁË,¶øJavaÔòÊÇÊäÈë"/**",ËüÒ²»á×Ô¶¯ÐγÉÏà¹Ø´úÂë,¾ßÌå´úÂëÈçÏÂ:
C#
/// <summary>
/// ÔÚ´ËÌîд×ÜÌåÃèÊö
///</summary>
/// <param name="name">ÕâÀïÌîд²ÎÊýnameµÄÃèÊö</param>
public string ......
Ê×ÏÈjava½Ó¿ÚºÍ³éÏóÀà´ú±íµÄ¾ÍÊdzéÏóÀàÐÍ£¬¾ÍÊÇÎÒÃÇÐèÒªÌá³ö³éÏó²ãµÄ¾ßÌåʵÏÖ£¬Èç¹ûÒªÌá¸ß³ÌÐòµÄ¸´ÓÃÂÊ£¬¿Éά»¤ÐÔ£¬¿ÉÀ©Õ¹ÐÔ£¬¾Í±ØÐëÃæÏò½Ó¿ÚºÍ³éÏó±à³Ì£¬ÕýȷʹÓÃËûÃÇ¡£ £¨1£©³éÏóÀà¿ÉÒÔÌṩʵÏÖ·½·¨£¬½Ó¿Ú²»ÄÜ
ÕâÊdzéÏóÀàµÄΨһÓŵ㣬¶øÇҷdz£ÓÐÓã¬ÀýÈ磬Ä㶨ÒåÒ»¸ö½Ó¿Ú£¬×ÓÀ಻ÐèÒªËûµÄËùÓз½·¨£¬¿ÉÊÇÄãû°ì·¨²»È¥ ......
ÖÚËùÖÜÖª£¬ÔÚJava¶àÏ̱߳à³ÌÖУ¬Ò»¸ö·Ç³£ÖØÒªµÄ·½Ãæ¾ÍÊÇÏ̵߳Äͬ²½ÎÊÌâ¡£
¹ØÓÚÏ̵߳Äͬ²½£¬Ò»°ãÓÐÒÔϽâ¾ö·½·¨£º
1.
ÔÚÐèҪͬ²½µÄ·½·¨µÄ·½·¨Ç©ÃûÖмÓÈësynchronized¹Ø¼ü×Ö¡£
2.
ʹÓÃsynchronized¿é¶ÔÐèÒª½øÐÐͬ²½µÄ´úÂë¶Î½øÐÐͬ²½¡£
3. ʹÓÃJDK
5ÖÐÌṩµÄjava.util.concurrent.lock°üÖеÄLock¶ÔÏó¡£ ......
package test;
/**
*
* @author openpk
*/
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class Main {
public static void main(String[] args) {
// Ï̳߳Ø
ExecutorService exec = Executors ......