JAVA多线程实现-实现Runnable接口

  1. 实现Runnable接口 implements Runnable
  1. 重写run()方法

@Override
public void run(){//TODO}

  1. 创建线程对象:Thread thread1 = new Thread(new ImplementsRunnable());

  2. 开启线程执行:thread1.start();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class ImplementsRunnable implements Runnable{
public static int num = 0;

@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + ": num = " + num++);
}
}
public static void main(String[] args) {
Thread thread1 = new Thread(new ImplementsRunnable());
Thread thread2 = new Thread(new ImplementsRunnable());
thread1.setName("线程1");
thread2.setName("线程2");
thread1.start();
thread2.start();
}
}