¥È¥Ã¥×   ÊÔ½¸ º¹Ê¬ ¥Ð¥Ã¥¯¥¢¥Ã¥× źÉÕ Ê£À½ ̾Á°Êѹ¹ ¥ê¥í¡¼¥É   ¿·µ¬ °ìÍ÷ ñ¸ì¸¡º÷ ºÇ½ª¹¹¿·   ¥Ø¥ë¥×   ºÇ½ª¹¹¿·¤ÎRSS

java/Thread ¤ÎÊѹ¹ÅÀ

Top / java / Thread

[[java]]

*extends Thread ¥µ¥ó¥×¥ë [#o8fe3c3e]
 class MyThread1 extends Thread {
 	public void run() {
 		for (int i = 1; i <= 10 ; i++) {
 			try {
 				sleep(500);
 				System.out.println("foo");
 			} catch (InterruptedException e) {
 			}
 		}
 	}
 }
 
 class MyThread2 extends Thread {
 	public void run() {
 		for (int i = 1; i <= 10 ; i++) {
 			try {
 				sleep(1000);
 				System.out.println("bar");
 			} catch (InterruptedException e) {
 			}
 		}
 	}
 }
 
 class test {
 	public static void main(String[] args) {
 		MyThread1 t1 = new MyThread1();
 		MyThread2 t2 = new MyThread2();
 		t1.start();
 		t2.start();
 	}
 }



*implements Runnable ³°Éô¤«¤é¥¹¥ì¥Ã¥É³«»Ï¥µ¥ó¥×¥ë [#v8f79806]
 class MyRunner implements Runnable {
 	public void run() {
 		for (int i = 1; i <= 5; i++) {
 			System.out.println("foo");
 			try {
 				Thread.sleep(1000);
 			} catch (InterruptedException e) {
 			}
 		}
 	}
 }
 
 class MyClass{
 	public static void main(String[] args) {
 		MyRunner r = new MyRunner();
 		Thread t = new Thread(r);
 		t.start();
 	}
 }



*implements Runnable ¼«¿È¤Î¥³¥ó¥¹¥È¥é¥¯¥¿¤Ç¥¹¥ì¥Ã¥É³«»Ï¥µ¥ó¥×¥ë [#ab0b4843]
 class MyRunner implements Runnable {
 	Thread t;
 
 	MyRunner() {
 		t = new Thread(this);
 		t.start();
 	}
 
 	public void run(){
 		for (int i = 1; i <= 5; i++) {
 			System.out.println("foo");
 			try {
 				Thread.sleep(1000);
 			} catch (InterruptedException e) {
 			}
 		}
 	}
 }
 
 class MyClass {
 	public static void main(String[] args) {
 		MyRunner r = new MyRunner();
 	}
 }