¡¾×ª¡¿Java synchronizedÓ÷¨ËѼ¯
ÔÚJavaÖУ¬synchronized¹Ø¼ü×ÖΪ·ÀÖ¹×ÊÔ´³åÍ»ÌṩÁËÖ§³Ö£¬Æä×÷ÓÃÓòÓжþÖÖ£º
ʵÀý·¶Î§¡£
¶ÔÏóʵÀý·¶Î§ÄÚsynchronizedʹÓõÄÁ½ÖÖÐÎʽ£º
ʵÀý·¶Î§Í¬²½·½·¨
publicd class syncTest {
…
synchronized void aMethod() {
//ÐèҪͬ²½Ê¹ÓõĴúÂë
}
}
synchronized aMethod(){}¿ÉÒÔ·ÀÖ¹¶à¸öÏß³Ìͬʱ·ÃÎÊÕâ¸ö¶ÔÏóʵÀýµÄsynchronized·½·¨£¨Èç¹ûÒ»¸ö¶ÔÏóÓжà¸ösynchronized·½·¨£¬Ö»ÒªÒ»¸öÏ̷߳ÃÎÊÁËÆäÖеÄÒ»¸ösynchronized·½·¨£¬ÆäËüÏ̲߳»ÄÜͬʱ·ÃÎÊÕâ¸ö¶ÔÏóÖÐÈκÎÒ»¸ösynchronized·½·¨£©¡£Õâʱ£¬²»Í¬µÄ¶ÔÏóʵÀýµÄsynchronized·½·¨ÊDz»Ïà¸ÉÈŵġ£Ò²¾ÍÊÇ˵£¬ÆäËüÏß³ÌÕÕÑù¿ÉÒÔͬʱ·ÃÎÊÏàͬÀàµÄÁíÒ»¸ö¶ÔÏóʵÀýÖеÄsynchronized·½·¨¡£
ʵÀý·¶Î§Í¬²½Çø¿é
publicd class syncTest {
…
void aMethod() {
//ÎÞÐèͬ²½Ê¹ÓõĴúÂë
…
synchronized(this) {
//ÐèҪͬ²½Ê¹ÓõĴúÂë¿é
}
}
}
³ýÁË·½·¨Ç°ÓÃsynchronized¹Ø¼ü×Ö£¬synchronized¹Ø¼ü×Ö»¹¿ÉÒÔÓÃÓÚ·½·¨ÖеÄij¸öÇø¿éÖУ¬±íʾֻ¶ÔÕâ¸öÇø¿éµÄ×ÊԴʵÐл¥³â·ÃÎÊ¡£Ó÷¨ÊÇ: synchronized(this){/*Çø¿é*/}£¬ËüµÄ×÷ÓÃÓòÊǵ±Ç°¶ÔÏó¡£
À෶Χ
ͬÑùµØ£¬ÔÚÀ෶ΧÄÚʹÓÃsynchronizedÒ²ÓÐͬ²½·½·¨ºÍͬ²½Çø¿éÁ½ÖÖÐÎʽ£º
À෶Χͬ²½·½·¨
publicd class syncTest {
…
synchronized static void aMethod() {
//ÐèÒªÔÚÀ෶Χͬ²½Ê¹ÓõĴúÂë
}
}
synchronized static aStaticMethod{}·ÀÖ¹¶à¸öÏß³Ìͬʱ·ÃÎÊÕâ¸öÀàÖеÄsynchronized static ·½·¨¡£Ëü¿ÉÒÔ¶ÔÀàµÄËùÓжÔÏóʵÀýÆð×÷Óá£Ò²¾ÍÊÇ˵ÔÚÒ»¸öJVMÖУ¬Í¬Ò»Ê±¼ä×î¶àÓÐÒ»¸ö¸ÃÀàµÄ¾²Ì¬Í¬²½·½·¨ÔÚÖ´ÐС£ÕâÑùµÄ·½·¨¿ÉÒÔÓ¦Óõ½¶àÏ̵߳Äͬ²½ÖУ¬ÊµÏÖ¸÷Ïß³ÌÖ±½ÓµÄÊý¾Ý¹²ÏíºÍ»¥¶¯¡£
À෶Χͬ²½Çø¿é
publicd class syncTest {
…
static void aMethod() {
…
//ÎÞÐèͬ²½Ê¹ÓõĴúÂë
synchronized (syncTest.class) {
//ÐèÒªÔÚÀ෶Χͬ²½Ê¹ÓõĴúÂë
}
}
}
À෶Χͬ²½Çø¿é¹¦ÄÜÓëÀ෶Χͬ²½·½·¨Ïàͬ£¬Ö»ÊǶÔÒ»¸öÇø¿éµÄ´úÂëµÄͬ²½¡£Ó÷¨ÊÇ£ºÔÚstatic ·½·¨ÄÚ¼Óͬ²½Çø¿é£ºsychronized (ÀàÃû.class) { /*Çø¿é*/}¡£
×¢ÒâÊÂÏî
synchronized¹Ø¼ü×ÖÊDz»Äܼ̳еÄ
synchronized¹Ø¼ü×ÖÊDz»Äܼ̳еģ¬Ò²¾ÍÊÇ˵£¬»ùÀàµÄ·½·¨synchronized f(){} Ôڼ̳ÐÀàÖв¢²»×Ô¶¯ÊÇsynchronized f(){}£¬¶øÊDZä³ÉÁËf(){}¡£¼Ì³ÐÀàÐèÒªÄãÏÔʽµÄÖ¸¶¨ËüµÄij¸ö·½·¨Îªsynchronized·
Ïà¹ØÎĵµ£º
ÀàµÄ³õʼ»¯ºÍ¶ÔÏó³õʼ»¯ÊÇ JVM ¹ÜÀíµÄÀàÐÍÉúÃüÖÜÆÚÖзdz£ÖØÒªµÄÁ½¸ö»·½Ú£¬Google ÁËÒ»±éÍøÂ磬ÓйØÀà×°ÔØ»úÖƵÄÎÄÕµ¹ÊDz»ÉÙ£¬È»¶øÀà³õʼ»¯ºÍ¶ÔÏó³õʼ»¯µÄÎÄÕ²¢²»¶à£¬ÌرðÊÇ´Ó×Ö½ÚÂëºÍ JVM ²ã´ÎÀ´·ÖÎöµÄÎÄÕ¸üÊÇÏÊÓÐËù¼û¡£
±¾ÎÄÖ÷Òª¶ÔÀàºÍ¶ÔÏó³õʼ»¯È«¹ý³Ì½øÐзÖÎö£¬Í¨¹ýÒ»¸öʵ¼ÊÎÊÌâÒýÈ룬½«Ô´´úÂëת»»³É JVM ×Ö½ÚÂëºó£¬ ......
package com.eastpro.batch.biz;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.security.spec.X509EncodedKeySpec;
public class SendMail {
private MimeMessage mimeMsg; //MIME mail object
private Session session;
  ......
/*
* ´´½¨ÈÕÆÚ 2007-03-14
*
* ¹¦ÄÜ È¡ÈÕÆÚʱ¼ä¹¤¾ß
*
*/
package com.util;
import java
.text.ParseException;
import java
.text.SimpleDateFormat;
import java
.util.Date;
import java
.util.Calendar;
import java
.util.GregorianCalendar;
/**
* ˵Ã÷: &nbs ......
µÚÒ»£¬Ì¸Ì¸final£¬ finally£¬ finalizeµÄÇø±ð¡£
¡¡¡¡final ÓÃÓÚÉùÃ÷ÊôÐÔ£¬·½·¨ºÍÀ࣬·Ö±ð±íʾÊôÐÔ²»¿É±ä£¬·½·¨²»¿É¸²¸Ç£¬À಻¿É¼Ì³Ð¡£finallyÊÇÒì³£´¦ÀíÓï¾ä½á¹¹µÄÒ»²¿·Ö£¬±íʾ×ÜÊÇÖ´ÐС£finalizeÊÇObjectÀàµÄÒ»¸ö·½·¨£¬ÔÚÀ¬»øÊÕ¼¯Æ÷Ö´ÐеÄʱºò»áµ÷Óñ»»ØÊÕ¶ÔÏóµÄ´Ë·½·¨£¬¿ÉÒÔ¸²¸Ç ......
Java ʵÓþÑé×ܽá --Swing ƪ
Ç°ÑÔ
±¾ÎÄÇ°ÑÔ²¿·ÖΪÎÒµÄһЩ¸ÐÏ룬Èç¹ûÄãÖ»¶Ô±¾ÎĽéÉÜµÄ Java ʵÓü¼ÇɸÐÐËȤ£¬¿ÉÒÔÌø¹ýÇ°ÑÔÖ±½Ó¿´ÕýÎĵÄÄÚÈÝ¡£
±¾ÎĵÄд×÷¶¯»úÀ´Ô´ÓÚ×î½ü½Ó¸øÈ˼ҰïæдµÄÒ»¸öС³ÌÐò£¬Ö÷ÒªÓÃÓÚ¹ÜÀí·ÖÆÚ¸¶¿îµÄ»õ¿îµÄһϵÁйÜÀí£¬°üÀ¨¹ýÆÚ¿îµÄ¼Í¼£¬¹ýÆÚ¿îÀûÏ¢µÄ¼ÆË㣬ΪÌáÇ°¸¶¿îµÄÓû§ÌṩһЩ·µ¿î½ ......