关于多线程的创建

import com.sun.deploy.ref.Helpers;

public class DuoXianCeng {
public static void main(String[] args) {
System.out.println("The main method was sdf:"+Thread.currentThread().getName());
Helper helper = new Helper("Java Thread Anywhere");


//创建一个线程
Thread thread = new Thread(helper);
//设置线程名字
thread.setName("A-Worker-Thread");
//启动线程
thread.start();
}

static class Helper implements Runnable {
private final String message;

public Helper(String message) {
this.message = message;
}

private void doSomething(String message) {
System.out.println("The doSomething method ws executed by thread:" + Thread.currentThread().getName());
System.out.println("Do something with" + message);
}

@Override
public void run() {
doSomething(message);
}

}
}

这是创建多线的流程的流程


* Thread用法

1、先继承Thread类,重写run()方法,将要运行的代码写到run()方法里;

2、实例化线程类,调用start()方法,开始线程;

3、start()方法被调用,执行的是线程类中重写后的run()方法内的代码

* Runnable用法

Thread t = new Thread(new RunnableDemo,"ThreadName");

t.start();




posted @ 2019-05-22 10:30  李肥驴  阅读(143)  评论(0编辑  收藏  举报