你的位置:首页 > Java教程

[Java教程]Java多线程实现


1.继承Thread类,由于Java单继承特性,此方法并不推荐。

2.实现Runnable接口,代码如下

class MyThread implements Runnable {    private String name;    public MyThread(String name) {    super();    this.name = name;  }    @Override  public void run() {    for(int i = 0 ; i < 200; i++) {      System.out.println("Thread"+name+"--->"+i);    }    }}public class ThreadDemo {  public static void main(String[] args) {    MyThread a = new MyThread("a");    MyThread b = new MyThread("b");    MyThread c = new MyThread("c");    new Thread(a).start();    new Thread(b).start();    new Thread(c).start();  }}