×î¼òµ¥µÄ±àд»ùÓÚÏ̵߳ĴúÂëµÄ·½·¨Ö®Ò»£¬¾ÍÊÇÅÉÉújava.lang.Thread Àà¡£¸ÃÏß³ÌÀàÊÇjava.lang °üµÄÒ»¸ö³ÉÔ±£¬ÔÚȱʡÇé¿öÏ£¬Ïß³ÌÀà¿ÉÒÔ±»ËùÓеÄJavaÓ¦ÓóÌÐòµ÷Óá£ÎªÁËʹÓÃÏß³ÌÀ࣬ÎÒÃÇÐèÒªÁ˽âThe java.lang.Thread ÀàÖж¨ÒåµÄÎå¸ö·½·¨£º
run()£º¸Ã·½·¨ÓÃÓÚÏ̵߳ÄÖ´ÐС£ÄãÐèÒªÖØÔظ÷½·¨£¬ÒÔ±ãÈÃÏß³Ì×öÌض¨µÄ¹¤×÷¡£
start()£º¸Ã·½·¨Ê¹µÃÏß³ÌÆô¶¯run()¡£
stop()£º¸Ã·½·¨Í¬start·½·¨µÄ×÷ÓÃÏà·´£¬Í£Ö¹Ï̵߳ÄÔËÐС£
suspend()£º¸Ã·½·¨Í¬stop·½·¨²»Í¬µÄÊÇ£¬Ëü²¢²»ÖÕֹδÍê³ÉµÄỊ̈߳¬Ëü½ö½ö¹ÒÆðỊ̈߳¬ÒԺ󻹿ɻָ´¡£
resume()£º¸Ã·½·¨ÖØÐÂÆô¶¯ÒѾ¹ÒÆðµÄÏ̡߳£
ÔËÐÐList AÖеijÌÐò£¬ÔËÐнá¹û¼ûList B
List A £ºÀ©Õ¹Ïß³ÌÀà
class TestThreads {
public static void main (String args []) {
class MyThread extends Thread {
String which;
MyThread (String which) {
this.which = which;
}
public void run() {
int iterations = (int)(Math.random()*100) %15;
int sleepinterval = (int)(Math.random()*1000);
System.out.println(which + " running for " + iterations +" iterations");
System.out.println(which + " sleeping for " + sleepinterval + "ms between loops");
for (int i = 0; < iterations; i++) {
System.out.println(which +" " + i);
try {
Thread.sleep(sleepinterval);
} catch (InterruptedException e) {}
}
}
}
MyThread a = new MyThread("Thread A");
MyThread b = new MyThread("Thread B");
MyThread c = new MyThread("Thread C");
a.start();
b.start();
c.start();
}
}
ListB: Çåµ¥AµÄÊä³ö
Thread A running for 16 iterations
Thread C running for 15 iterations
Thread B running for 14 iterations
Thread A sleeping for 305ms between
loops
Thread C sleeping for 836ms between
loops
Thread B sleeping for 195ms between
loops
Thread A 0
Thread C 0
Thread B 0
. . .
Thread C 13
Thread B 13
Thread A 14
Thread C 14
Thread A 15
List AÑÝʾÁËÈçºÎ´ÓÏÖÓеÄThreadÀàÖÐÅÉÉú³öÒ»¸öÐÂÀࡣд´½¨µÄÀàÖØÔØÁËrun ·½·¨¡£ÓÐȤµÄÊÇ£¬ÊµÏÖrun ·½·¨²»±ØºÜÑϸñ£¬ÒòΪThreadÀàÌṩһ¸öȱʡµÄrun·½·¨£¬¾¡¹ÜËü²»ÊÇÌرðÓÐÓá£
ÔÚÓÐЩ³¡ºÏ£¬ÎÒÃDz»Äܼòµ¥µØ¸Ä±äÖ¸¶¨¶ÔÏóµÄ¸¸Àà¡£ÎÒÃÇÈÔÈ»ÐèÒª²ÉÓÃÏ̡߳£Õâʱ£¬ÎÒÃǾÍÐèÒªÓõ½Runnable½Ó¿Ú¡£