ºìÁªLinuxÃÅ»§
Linux°ïÖú

JAVA»ù´¡£ºJava ThreadÓ¦¸Ã×¢ÒâµÄÎÊÌâ

·¢²¼Ê±¼ä:2006-06-15 10:24:18À´Ô´:ºìÁª×÷Õß:[Hl.y]
JavaµÄÏ̱߳à³Ì·Ç³£¼òµ¥¡£µ«ÓÐʱ»á¿´µ½Ò»Ð©¹ØÓÚÏ̵߳ĴíÎóÓ÷¨¡£ÏÂÃæÁгöһЩӦ¸Ã×¢ÒâµÄÎÊÌâ¡£

1£®Í¬²½¶ÔÏóµÄºã¶¨ÐÔ
All java objects are references.

¶ÔÓÚ¾Ö²¿±äÁ¿ºÍ²ÎÊýÀ´Ëµ£¬javaÀïÃæµÄint, float, double, booleanµÈ»ù±¾Êý¾ÝÀàÐÍ£¬¶¼ÔÚÕ»ÉÏ¡£ÕâЩ»ù±¾ÀàÐÍÊÇÎÞ·¨Í¬²½µÄ£»javaÀïÃæµÄ¶ÔÏ󣨸ù¶ÔÏóÊÇObject£©£¬È«¶¼ÔÚ¶ÑÀָÏò¶ÔÏóµÄreferenceÔÚÕ»ÉÏ¡£

javaÖеÄͬ²½¶ÔÏó£¬Êµ¼ÊÉÏÊǶÔÓÚreferenceËùÖ¸µÄ¡°¶ÔÏóµØÖ·¡±½øÐÐͬ²½¡£
ÐèҪעÒâµÄÎÊÌâÊÇ£¬Ç§Íò²»Òª¶Ôͬ²½¶ÔÏóÖØи³Öµ¡£¾Ù¸öÀý×Ó¡£
class A implements Runnable{
Object lock = new Object();

void run(){
for(...){
synchronized(lock){
// do something
...
lock = new Object();
}
}
}

runº¯ÊýÀïÃæµÄÕâ¶Îͬ²½´úÂëʵ¼ÊÉÏÊǺÁÎÞÒâÒåµÄ¡£ÒòΪÿһ´Îlock¶¼¸øÖØзÖÅäÁËеĶÔÏóµÄreference£¬Ã¿¸öÏ̶߳¼ÔÚеÄreferenceͬ²½¡£
´ó¼Ò¿ÉÄܾõµÃÆæ¹Ö£¬Ôõô»á¾ÙÕâôһ¸öÀý×Ó¡£ÒòΪÎÒ¼û¹ýÕâÑùµÄ´úÂ룬ͬ²½¶ÔÏóÔÚÆäËüµÄº¯ÊýÀï±»ÖØи³ÁËÐÂÖµ¡£
ÕâÖÖÎÊÌâºÜÄѲé³öÀ´¡£
ËùÒÔ£¬Ò»°ãÓ¦¸Ã°Ñͬ²½¶ÔÏóÉùÃ÷Ϊfinal.
final Object lock = new Object();



ʹÓÃSingleton Pattern Éè¼ÆģʽÀ´»ñȡͬ²½¶ÔÏó£¬Ò²ÊÇÒ»ÖֺܺõÄÑ¡Ôñ¡£

2£®ÈçºÎ·ÅÖù²ÏíÊý¾Ý
ʵÏÖỊ̈߳¬ÓÐÁ½ÖÖ·½·¨£¬Ò»ÖÖÊǼ̳ÐThreadÀ࣬һÖÖÊÇʵÏÖRunnable½Ó¿Ú¡£

ÉÏÃæ¾ÙµÄÀý×Ó£¬²ÉÓÃʵÏÖRunnable½Ó¿ÚµÄ·½·¨¡£±¾ÎÄÍƼöÕâÖÖ·½·¨¡£

Ê×ÏÈ£¬°ÑÐèÒª¹²ÏíµÄÊý¾Ý·ÅÔÚÒ»¸öʵÏÖRunnable½Ó¿ÚµÄÀàÀïÃ棬Ȼºó£¬°ÑÕâ¸öÀàµÄʵÀý´«¸ø¶à¸öThreadµÄ¹¹Ôì·½·¨¡£ÕâÑù£¬Ð´´½¨µÄ¶à¸öThread£¬¶¼¹²Í¬ÓµÓÐÒ»¸öRunnableʵÀý£¬¹²Ïíͬһ·ÝÊý¾Ý¡£

Èç¹û²ÉÓü̳ÐThreadÀàµÄ·½·¨£¬¾ÍÖ»ºÃʹÓÃstatic¾²Ì¬³ÉÔ±ÁË¡£Èç¹û¹²ÏíµÄÊý¾Ý±È½Ï¶à£¬¾ÍÐèÒª´óÁ¿µÄstatic¾²Ì¬³ÉÔ±£¬Áî³ÌÐòÊý¾Ý½á¹¹»ìÂÒ£¬ÄÑÒÔÀ©Õ¹¡£ÕâÖÖÇé¿öÓ¦¸Ã¾¡Á¿±ÜÃâ¡£

±àдһ¶Î¶àÏ̴߳úÂ룬´¦ÀíÒ»¸öÉÔ΢¸´ÔÓµãµÄÎÊÌâ¡£Á½ÖÖ·½·¨µÄÓÅÁÓ£¬Ò»ÊÔ±ãÖª¡£

3£®Í¬²½µÄÁ£¶È
Ïß³Ìͬ²½µÄÁ£¶ÈԽСԽºÃ£¬¼´£¬Ïß³Ìͬ²½µÄ´úÂë¿éԽСԽºÃ¡£¾¡Á¿±ÜÃâÓÃsynchronizedÐÞÊηûÀ´ÉùÃ÷·½·¨¡£¾¡Á¿Ê¹ÓÃsynchronized(anObject)µÄ·½Ê½£¬Èç¹û²»ÏëÒýÈëеÄͬ²½¶ÔÏó£¬Ê¹ÓÃsynchronized(this)µÄ·½Ê½¡£¶øÇÒ£¬synchronized´úÂë¿éԽСԽºÃ¡£

4£®Ïß³ÌÖ®¼äµÄ֪ͨ
ÕâÀïʹÓá°Í¨Öª¡±Õâ¸ö´Ê£¬¶ø²»Óá°Í¨ÐÅ¡±Õâ¸ö´Ê£¬ÊÇΪÁ˱ÜÃâ´ÊÒåµÄÀ©´ó»¯¡£

Ïß³ÌÖ®¼äµÄ֪ͨ£¬Í¨¹ýObject¶ÔÏóµÄwait()ºÍnotify() »ònotifyAll() ·½·¨ÊµÏÖ¡£

ÏÂÃæÓÃÒ»¸öÀý×Ó£¬À´ËµÃ÷Æ乤×÷Ô­Àí£º

¼ÙÉèÓÐÁ½¸öỊ̈߳¬AºÍB¡£¹²Í¬ÓµÓÐÒ»¸öͬ²½¶ÔÏó£¬lock¡£

1£®Ê×ÏÈ£¬Ïß³ÌAͨ¹ýsynchronized(lock) »ñµÃlockͬ²½¶ÔÏó£¬È»ºóµ÷ÓÃlock.wait()º¯Êý£¬·ÅÆúlockͬ²½¶ÔÏó£¬Ïß³ÌAÍ£Ö¹ÔËÐУ¬½øÈëµÈ´ý¶ÓÁС£

2£®Ïß³ÌBͨ¹ýsynchronized(lock) »ñµÃÏß³ÌA·ÅÆúµÄlockͬ²½¶ÔÏó£¬×öÍêÒ»¶¨µÄ´¦Àí£¬È»ºóµ÷Óà lock.notify() »òÕßlock.notifyAll() ֪ͨµÈ´ý¶ÓÁÐÀïÃæµÄÏß³ÌA¡£

3£®Ïß³ÌA´ÓµÈ´ý¶ÓÁÐÀïÃæ³öÀ´£¬½øÈëready¶ÓÁУ¬µÈ´ýµ÷¶È¡£

4£®Ïß³ÌB¼ÌÐø´¦Àí£¬³öÁËsynchronized(lock)¿éÖ®ºó£¬·ÅÆúlockͬ²½¶ÔÏó¡£

5£®Ïß³ÌA»ñµÃlockͬ²½¶ÔÏ󣬼ÌÐøÔËÐС£



Àý×Ó´úÂëÈçÏ£º

public class SharedResource implements Runnable{

Object lock = new Object();



public void run(){

// »ñÈ¡µ±Ç°Ï̵߳ÄÃû³Æ¡£

String threadName = Thread.currentThread().getName();



if( ¡°A¡±.equals(threadName)){

synchronized(lock){ //Ïß³ÌAͨ¹ýsynchronized(lock) »ñµÃlockͬ²½¶ÔÏó

try{

System.out.println(¡° A gives up lock.¡±);

lock.wait(); // µ÷ÓÃlock.wait()º¯Êý£¬·ÅÆúlockͬ²½¶ÔÏó£¬

// Ïß³ÌAÍ£Ö¹ÔËÐУ¬½øÈëµÈ´ý¶ÓÁС£

}catch(InterruptedException e){

}



// Ïß³ÌAÖØлñµÃlockͬ²½¶ÔÏóÖ®ºó£¬¼ÌÐøÔËÐС£

System.out.println(¡° A got lock again and continue to run.¡±);

} // end of synchronized(lock)

}



if( ¡°B¡±.equals(threadName)){

synchronized(lock){//Ïß³ÌBͨ¹ýsynchronized(lock) »ñµÃÏß³ÌA·ÅÆúµÄlockͬ²½¶ÔÏó

System.out.println(¡°B got lock.¡±);



lock.notify(); //֪ͨµÈ´ý¶ÓÁÐÀïÃæµÄÏß³ÌA£¬½øÈëready¶ÓÁУ¬µÈ´ýµ÷¶È¡£



//Ïß³ÌB¼ÌÐø´¦Àí£¬³öÁËsynchronized(lock)¿éÖ®ºó£¬·ÅÆúlockͬ²½¶ÔÏó¡£

System.out.println(¡°B gives up lock.¡±);

} // end of synchronized(lock)



boolean hasLock = Thread.holdsLock(lock); // ¼ì²éBÊÇ·ñÓµÓÐlockͬ²½¶ÔÏó¡£

System.out.println(¡°B has lock ? -- ¡± +hasLock); // false.

}

}

}



public class TestMain{

public static void main(){

Runnable resource = new SharedResource();



Thread A = new Thread(resource£¬¡±A¡±);

A.start();



// Ç¿ÆÈÖ÷Ïß³ÌÍ£Ö¹ÔËÐУ¬ÒÔ±ãÏß³ÌA¿ªÊ¼ÔËÐС£

¡¡¡¡try {

Thread.sleep(500);

}catch(InterruptedException e){

}



Thread B = new Thread(resource£¬¡±B¡±);

B.start();

}

}



5£®¿çÀàµÄͬ²½¶ÔÏó
¶ÔÓÚ¼òµ¥µÄÎÊÌ⣬¿ÉÒÔ°Ñ·ÃÎʹ²Ïí×ÊÔ´µÄͬ²½´úÂ붼·ÅÔÚÒ»¸öÀàÀïÃæ¡£

µ«ÊǶÔÓÚ¸´ÔÓµÄÎÊÌ⣬ÎÒÃÇÐèÒª°ÑÎÊÌâ·ÖΪ¼¸¸ö²¿·ÖÀ´´¦Àí£¬ÐèÒª¼¸¸ö²»Í¬µÄÀàÀ´´¦ÀíÎÊÌâ¡£Õâʱ£¬¾ÍÐèÒªÔÚ²»Í¬µÄÀàÖУ¬¹²Ïíͬ²½¶ÔÏó¡£±ÈÈ磬ÔÚÉú²úÕߺÍÏû·ÑÕßÖ®¼ä¹²Ïíͬ²½¶ÔÏó£¬ÔÚ¶ÁÕߺÍдÕßÖ®¼ä¹²Ïíͬ²½¶ÔÏó¡£

ÈçºÎÔÚ²»Í¬µÄÀàÖУ¬¹²Ïíͬ²½¶ÔÏó¡£Óм¸ÖÖ·½·¨ÊµÏÖ£¬

£¨1£©Ç°Ãæ½²¹ýµÄ·½·¨£¬Ê¹ÓÃstatic¾²Ì¬³ÉÔ±£¬£¨»òÕßʹÓÃSingleton Pattern.£©

£¨2£©ÓòÎÊý´«µÝµÄ·½·¨£¬°Ñͬ²½¶ÔÏ󴫵ݸø²»Í¬µÄÀà¡£

£¨3£©ÀûÓÃ×Ö·û´®³£Á¿µÄ¡°Ô­×ÓÐÔ¡±¡£



¶ÔÓÚµÚÈýÖÖ·½·¨£¬ÕâÀï×öһϽâÊÍ¡£Ò»°ãÀ´Ëµ£¬³ÌÐò´úÂëÖеÄ×Ö·û´®³£Á¿¾­¹ý±àÒëÖ®ºó£¬¶¼¾ßÓÐΨһÐÔ£¬¼´£¬ÄÚ´æÖв»»á´æÔÚÁ½·ÝÏàͬµÄ×Ö·û´®³£Á¿¡£

£¨Í¨³£Çé¿öÏ£¬C++£¬CÓïÑÔ³ÌÐò±àÒëÖ®ºó£¬Ò²¾ßÓÐͬÑùµÄÌØÐÔ¡££©

±ÈÈ磬ÎÒÃÇÓÐÈçÏ´úÂë¡£

String A = ¡°atom¡±;

String B = ¡°atom¡±;

ÎÒÃÇÓÐÀíÓÉÈÏΪ£¬AºÍBÖ¸Ïòͬһ¸ö×Ö·û´®³£Á¿¡£¼´£¬A==B¡£

×¢Ò⣬ÉùÃ÷×Ö·û´®±äÁ¿µÄ´úÂ룬²»·ûºÏÉÏÃæµÄ¹æÔò¡£

String C= new String(¡°atom¡±);

String D = new String(¡°atom¡±);

ÕâÀïµÄCºÍDµÄÉùÃ÷ÊÇ×Ö·û´®±äÁ¿µÄÉùÃ÷£¬ËùÒÔ£¬C != D¡£



ÓÐÁËÉÏÊöµÄÈÏʶ£¬ÎÒÃǾͿÉÒÔʹÓÃ×Ö·û´®³£Á¿×÷Ϊͬ²½¶ÔÏó¡£

±ÈÈçÎÒÃÇÔÚ²»Í¬µÄÀàÖУ¬Ê¹ÓÃsynchronized(¡°myLock¡±), ¡°myLock¡±.wait(),¡°myLock¡±.notify(), ÕâÑùµÄ´úÂ룬¾ÍÄܹ»ÊµÏÖ²»Í¬ÀàÖ®¼äµÄÏß³Ìͬ²½¡£

±¾ÎIJ¢²»Ç¿ÁÒÍƼöÕâÖÖÓ÷¨£¬Ö»ÊÇ˵Ã÷£¬ÓÐÕâÑùÒ»ÖÖ·½·¨´æÔÚ¡£



±¾ÎÄÍƼöµÚ¶þÖÖ·½·¨£¬£¨2£©ÓòÎÊý´«µÝµÄ·½·¨£¬°Ñͬ²½¶ÔÏ󴫵ݸø²»Í¬µÄÀà¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ