ユーチューブ動画
マルチスレッドについて解説します。
ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
public class ThreadTest { public static void main(String[] args) { Thread thread1 = new Thread(new Thread1()); Thread thread2 = new Thread(new Thread2()); thread1.start(); thread2.start(); } } class Thread1 implements Runnable { public void run() { while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Thread1"); } } } class Thread2 implements Runnable { public void run() { while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Thread2"); } } } |
このサンプルコードをJavaタッチタイプゲームとして遊ぶことができます。