(CSDN迁移)JAVA多线程实现-继承Thread

  1. 继承Thread方法:
extends Thread
  1. 重写覆盖run()方法:
@Override
public void run()
  1. 通过start()方法启动线程。
threadDemo01.start();
  1. 若需要向线程中传递参数,可以采用在线程类(如例子中的ExtendThread)定义成员变量,成员变量可以是基本类型,也可以是其他类,例如,可以在run方法中回调成员变量的方法。
public class ExtendThread extends Thread{
    
    public static int ii = 0;
    public MyPrint = new MyPrint()
    @Override
    public void run(){
        //编写自己的线程代码
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + ": i = " + ii++);
            try {
                sleep(Math.round(Math.random()*100));
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
    
    public static void main(String[] args){ 
        ExtendThread threadDemo01 = new ExtendThread();
        ExtendThread threadDemo02 = new ExtendThread(); 
        threadDemo01.setName("线程1");
        threadDemo02.setName("线程2");
        threadDemo01.start();     
        threadDemo02.start();
    }
}

更简便的实现方式则是直接在插入线程的地方添加代码:(并不推荐)

final String string = ...;
final int ddd = ...;
new Thread(){
    public void run() {
             //TODO in the new Thread
    };
}.start();

猜你喜欢

转载自www.cnblogs.com/AbelZone/p/10064125.html