JavaỊ̈߳ºÐÂÌØÕ÷ Ô×ÓÁ¿
JavaỊ̈߳ºÐÂÌØÕ÷-Ô×ÓÁ¿
ËùνµÄÔ×ÓÁ¿¼´²Ù×÷±äÁ¿µÄ²Ù×÷ÊÇ“Ô×ӵĔ£¬¸Ã²Ù×÷²»¿ÉÔÙ·Ö£¬Òò´ËÊÇḬ̈߳²È«µÄ¡£
ΪºÎҪʹÓÃÔ×Ó±äÁ¿ÄØ£¬ÔÒòÊǶà¸öÏ̶߳Ե¥¸ö±äÁ¿²Ù×÷Ò²»áÒýÆðһЩÎÊÌâ¡£ÔÚJava5֮ǰ£¬¿ÉÒÔͨ¹ývolatile¡¢synchronized¹Ø¼ü×ÖÀ´½â¾ö²¢·¢·ÃÎʵݲȫÎÊÌ⣬µ«ÕâÑùÌ«Âé·³¡£
Java5Ö®ºó£¬×¨ÃÅÌṩÁËÓÃÀ´½øÐе¥±äÁ¿¶àÏ̲߳¢·¢°²È«·ÃÎʵŤ¾ß°üjava.util.concurrent.atomic£¬ÆäÖеÄÀàÒ²ºÜ¼òµ¥¡£
ÏÂÃæ¸ø³öÒ»¸ö·´ÃæÀý×Ó£¨ÇÐÎðÄ£·Â£©£º
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;
/**
* JavaỊ̈߳ºÐÂÌØÕ÷-Ô×ÓÁ¿
*
* @author leizhimin 2009-11-6 9:53:11
*/
public class Test {
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(2);
Runnable t1 = new MyRunnable("ÕÅÈý", 2000);
Runnable t2 = new MyRunnable("ÀîËÄ", 3600);
Runnable t3 = new MyRunnable("ÍõÎå", 2700);
Runnable t4 = new MyRunnable("ÀÏÕÅ", 600);
Runnable t5 = new MyRunnable("ÀÏÅ£", 1300);
Runnable t6 = new MyRunnable("ÅÖ×Ó", 800);
//Ö´Ðи÷¸öÏß³Ì
pool.execute
Ïà¹ØÎĵµ£º
JavaỊ̈߳ºÏß³Ì״̬µÄת»»
SCJP5ѧϰ±Ê¼Ç
Ò»¡¢Ïß³Ì״̬
Ï̵߳Ä״̬ת»»ÊÇÏ߳̿ØÖƵĻù´¡¡£Ïß³Ì״̬×ܵĿɷÖΪÎå´ó״̬£º·Ö±ðÊÇÉú¡¢ËÀ¡¢¿ÉÔËÐС¢ÔËÐС¢µÈ´ý/×èÈû¡£ÓÃÒ»¸öͼÀ´ÃèÊöÈçÏ£º
1¡¢ÐÂ״̬£ºÏ̶߳ÔÏóÒѾ´´½¨£¬»¹Ã»ÓÐÔÚÆäÉϵ÷ÓÃstart()·½·¨¡£
2¡¢¿ÉÔËÐÐ״̬£ºµ±Ïß³ÌÓÐ× ......
JavaỊ̈߳ºÏ̵߳ĵ÷¶È-Èò½
Ï̵߳ÄÈò½º¬Òå¾ÍÊÇʹµ±Ç°ÔËÐÐ×ÅÏß³ÌÈóöCPU×ÊÔ´£¬µ«ÊÇÈ»¸ø˲»ÖªµÀ£¬½ö½öÊÇÈóö£¬Ïß³Ì״̬»Øµ½¿ÉÔËÐÐ״̬¡£
Ï̵߳ÄÈò½Ê¹ÓÃThread.yield()·½·¨£¬yield() Ϊ¾²Ì¬·½·¨£¬¹¦ÄÜÊÇÔÝÍ£µ±Ç°ÕýÔÚÖ´ÐеÄÏ̶߳ÔÏ󣬲¢Ö´ÐÐÆäËûÏ̡߳£
/**
* JavaỊ̈߳ºÏ̵߳ĵ÷¶È-Èò½
*
......
JavaỊ̈߳ºÏ̵߳ĵ÷¶È-ºÏ²¢
Ï̵߳ĺϲ¢µÄº¬Òå¾ÍÊǽ«¼¸¸ö²¢ÐÐÏ̵߳ÄÏ̺߳ϲ¢ÎªÒ»¸öµ¥Ïß³ÌÖ´ÐУ¬Ó¦Óó¡¾°Êǵ±Ò»¸öÏ̱߳ØÐëµÈ´ýÁíÒ»¸öÏß³ÌÖ´ÐÐÍê±Ï²ÅÄÜÖ´ÐÐʱ¿ÉÒÔʹÓÃjoin·½·¨¡£
joinΪ·Ç¾²Ì¬·½·¨£¬¶¨ÒåÈçÏ£º
void join()
µÈ´ý¸ÃÏß³ÌÖÕÖ¹¡£ ......
JavaỊ̈߳ºÏ̵߳ĵ÷¶È-ÊØ»¤Ïß³Ì
ÊØ»¤Ïß³ÌÓëÆÕͨÏß³Ìд·¨ÉÏ»ù±¾Ã´É¶Çø±ð£¬µ÷ÓÃÏ̶߳ÔÏóµÄ·½·¨setDaemon(true)£¬Ôò¿ÉÒÔ½«ÆäÉèÖÃΪÊØ»¤Ï̡߳£
ÊØ»¤Ïß³ÌʹÓõÄÇé¿ö½ÏÉÙ£¬µ«²¢·ÇÎÞÓ㬾ÙÀýÀ´Ëµ£¬JVMµÄÀ¬»ø»ØÊÕ¡¢ÄÚ´æ¹ÜÀíµÈÏ̶߳¼ÊÇÊØ»¤Ï̡߳£»¹ÓоÍÊÇÔÚ×öÊý¾Ý¿âÓ¦ÓÃʱºò£¬Ê¹ÓõÄÊý¾Ý¿âÁ¬½Ó³Ø£¬Á¬½Ó³Ø±¾ÉíÒ²°üº ......
JavaỊ̈߳ºÐÂÌØÕ÷-Ëø£¨ÉÏ£©
ÔÚJava5ÖУ¬×¨ÃÅÌṩÁËËø¶ÔÏó£¬ÀûÓÃËø¿ÉÒÔ·½±ãµÄʵÏÖ×ÊÔ´µÄ·âËø£¬ÓÃÀ´¿ØÖƶԾºÕù×ÊÔ´²¢·¢·ÃÎʵĿØÖÆ£¬ÕâЩÄÚÈÝÖ÷Òª¼¯ÖÐÔÚjava.util.concurrent.locks °üÏÂÃ棬ÀïÃæÓÐÈý¸öÖØÒªµÄ½Ó¿ÚCondition¡¢Lock¡¢ReadWriteLock¡£
Condition
Condition ½« Object ¼àÊÓÆ÷·½·¨£¨wait¡¢notify ......