一、通过构造函数传递参数
public class MyThread1 extends Thread{ private String name; public MyThread1(String name) { this.name = name; } public void run() { System.out.println("hello " + name); } public static void main(String[] args) { Thread thread = new MyThread1("world"); thread.start(); }}
二、通过变量和方法传递数据
public class MyThread2 implements Runnable{ private String name; public void setName(String name) { this.name = name; } public void run() { System.out.println("hello " + name); } public static void main(String[] args) { MyThread2 myThread = new MyThread2(); myThread.setName("world"); Thread thread = new Thread(myThread); thread.start(); }}
三、通过回调函数传递数据
转载: