Runnable接口程实现多线程
写一个类实现Runnable接口,实现run方法。用new Thread(Runnableclass).start()方法来启动
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 RunnableTask { public static void main(String[] args) { RunnableTask rt = new RunnableTask(); Runner1 runner1 = rt.new Runner1(); Runner2 runner2 = rt.new Runner2(); Thread thread1 = new Thread(runner1); Thread thread2 = new Thread(runner2); thread1.start(); thread2.start(); } class Runner1 implements Runnable { public void run() { for (int i = 0; i < 100; i++) { System.out.println("进入Runner1运行状态——————————" + i); } } } class Runner2 implements Runnable { public void run() { for (int i = 0; i < 100; i++) { System.out.println("进入Runner2运行状态==========" + i); } } } }
|