JavaỊ̈߳ºÏ̵߳Äͬ²½ÓëËø
JavaỊ̈߳ºÏ̵߳Äͬ²½ÓëËø
Ò»¡¢Í¬²½ÎÊÌâÌá³ö
Ï̵߳Äͬ²½ÊÇΪÁË·ÀÖ¹¶à¸öÏ̷߳ÃÎÊÒ»¸öÊý¾Ý¶ÔÏóʱ£¬¶ÔÊý¾ÝÔì³ÉµÄÆÆ»µ¡£
ÀýÈ磺Á½¸öÏß³ÌThreadA¡¢ThreadB¶¼²Ù×÷ͬһ¸ö¶ÔÏóFoo¶ÔÏ󣬲¢ÐÞ¸ÄFoo¶ÔÏóÉϵÄÊý¾Ý¡£
public class Foo {
private int x = 100;
public int getX() {
return x;
}
public int fix(int y) {
x = x - y;
return x;
}
}
public class MyRunnable implements Runnable {
private Foo foo = new Foo();
public static void main(String[] args) {
MyRunnable r = new MyRunnable();
Thread ta = new Thread(r, "Thread-A");
Thread tb = new Thread(r, "Thread-B");
ta.start();
tb.start();
}
public void run() {
for (int i = 0; i < 3; i++) {
this.fix(30);
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
&nb
Ïà¹ØÎĵµ£º
JAVA·´Éä»úÖÆÊÇÔÚÔËÐÐ״̬ÖУ¬¶ÔÓÚÈÎÒâÒ»¸öÀ࣬¶¼Äܹ»ÖªµÀÕâ¸öÀàµÄËùÓÐÊôÐԺͷ½·¨£»¶ÔÓÚÈÎÒâÒ»¸ö¶ÔÏ󣬶¼Äܹ»µ÷ÓÃËüµÄÈÎÒâÒ»¸ö·½·¨£»ÕâÖÖ¶¯Ì¬»ñÈ¡µÄÐÅÏ¢ÒÔ¼°¶¯Ì¬µ÷ÓöÔÏóµÄ·½·¨µÄ¹¦ÄܳÆΪjavaÓïÑԵķ´Éä»úÖÆ¡£
package mypakage;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; ......
Éè¼Æģʽ
Ò»¸ö³ÌÐòÔ±¶ÔÉè¼ÆģʽµÄÀí½â:
“²»¶®”ΪʲôҪ°ÑºÜ¼òµ¥µÄ¶«Î÷¸ãµÃÄÇô¸´ÔÓ¡£ºóÀ´Ëæ×ÅÈí¼þ¿ª·¢¾ÑéµÄÔö¼Ó²Å¿ªÊ¼Ã÷°×ÎÒËù¿´µ½µÄ“¸´ÔӔǡǡ¾ÍÊÇÉè¼ÆģʽµÄ¾«ËèËùÔÚ£¬ÎÒËùÀí½âµÄ“¼òµ¥”¾ÍÊÇÒ»°ÑÔ¿³×¿ªÒ»°ÑËøµÄģʽ£¬Ä¿µÄ½ö½öÊÇ×ÅÑÛÓÚ½â¾öÏÖÔÚµÄÎÊÌ⣬¶øÉè¼ÆģʽµÄ“¸´ÔÓ&rdq ......
±¾ÎÄÊ×ÏÈÉè¼ÆÒ»¸öµ¥´Î¶¨Ê±Æ÷Timer+TimerTask£¬È»ºóÔٴλù´¡ÉÏÉè¼ÆÒ»¸öÑ»·¶¨Ê±Æ÷¡£
ÎÒ½«°Ñ java.util.Timer ºÍ java.util.TimerTask ͳ³ÆΪ Java ¼ÆʱÆ÷¿ò¼Ü£¬ËüÃÇʹ³ÌÐòÔ±¿ÉÒÔºÜÈÝÒ׵ؼƻ®¼òµ¥µÄÈÎÎñ£¨×¢ÒâÕâЩÀàÒ²¿ÉÓÃÓÚ J2ME ÖУ©¡£²»¹ý£¬Java ¼ÆʱÆ÷¿ò¼ÜûÓÐ× ......
ÓÃÔÚ¶àỊ̈߳¬Í¬²½±äÁ¿¡£ Ïß³ÌΪÁËÌá¸ßЧÂÊ£¬½«Ä³³ÉÔ±±äÁ¿(ÈçA)¿½±´ÁËÒ»·Ý£¨ÈçB£©£¬Ïß³ÌÖжÔAµÄ·ÃÎÊÆäʵ·ÃÎʵÄÊÇB¡£Ö»ÔÚijЩ¶¯×÷ʱ²Å½øÐÐAºÍBµÄͬ²½¡£Òò´Ë´æÔÚAºÍB²»Ò»ÖµÄÇé¿ö¡£volatile¾ÍÊÇÓÃÀ´±ÜÃâÕâÖÖÇé¿öµÄ¡£volatile¸æËßjvm£¬ ËüËùÐÞÊεıäÁ¿²»±£Áô¿½±´£¬Ö±½Ó·ÃÎÊÖ÷ÄÚ´æÖеģ¨Ò²¾ÍÊÇÉÏÃæ˵µÄA)
===================== ......
JavaỊ̈߳º´´½¨ÓëÆô¶¯
SCJP5ѧϰ±Ê¼Ç
Ò»¡¢¶¨ÒåÏß³Ì
1¡¢À©Õ¹java.lang.ThreadÀà¡£
´ËÀàÖÐÓиörun()·½·¨£¬Ó¦¸Ã×¢ÒâÆäÓ÷¨£º
public void run()
Èç¹û¸ÃÏß³ÌÊÇʹÓöÀÁ¢µÄ Runnable ÔËÐжÔÏó¹¹ÔìµÄ£¬Ôòµ÷Óøà Runnable ¶ÔÏóµÄ run ·½·¨£»·ñÔò£¬¸Ã·½·¨²»Ö´ÐÐÈκβÙ×÷²¢·µ»Ø¡£
Thr ......