创建多线程的方式二:实现Runnable接口

 1 package com.yhqtv.java;
 2 
 3 /*创建多线程的方式二:实现Runnable接口
 4  * 1.创建一个实现了Runnable接口的类
 5  * 2.实现类去实现Runnable中的抽学方法:run()
 6  * 3.创建实现类的对象
 7  * 4.将此对象作为参数传递到Thread类的构造其中,创建Thread类的对象
 8  * 5.通过Thread类的对象调用start()
 9  *
10  * @author  XMKJ  yhqtv.com Email:[email protected]
11  * @create 2020-04-28-18:12
12  *
13  */
14 class MThread implements Runnable{
15     @Override
16     public void run() {
17         for (int i = 0; i < 100; i++) {
18             if(i%2==0){
19                 System.out.println(Thread.currentThread().getName()+":"+i);
20             }
21         }
22     }
23 }
24 
25 public class ThreadTest1 {
26     public static void main(String[] args) {
27         MThread mt=new MThread();
28         Thread t1 = new Thread(mt);
29         t1.setName("线程1");
30         t1.start();
31 
32         Thread t2 = new Thread(mt);
33         t2.setName("线程2");
34         t2.start();
35     }
36 }

猜你喜欢

转载自www.cnblogs.com/yhqtv-com/p/12796403.html