在傳統的同步開發模式下
欲先取之
一
在創建線程時
package mythread;
public class MyThread
{
private String name;
public MyThread
{
this
}
public void run()
{
System
}
public static void main(String[] args)
{
Thread thread = new MyThread
thread
}
}
由於這種方法是在創建線程對象的同時傳遞數據的
二
向對象中傳入數據一般有兩次機會
package mythread;
public class MyThread
{
private String name;
public void setName(String name)
{
this
}
public void run()
{
System
}
public static void main(String[] args)
{
MyThread
myThread
Thread thread = new Thread(myThread);
thread
}
}
三
上面討論的兩種向線程中傳遞數據的方法是最常用的
package mythread;
class Data
{
public int value =
}
class Work
{
public void process(Data data
{
for (int n : numbers)
{
data
}
}
}
public class MyThread
{
private Work work;
public MyThread
{
this
}
public void run()
{
java
Data data = new Data();
int n
int n
int n
work
System
+ String
}
public static void main(String[] args)
{
Thread thread = new MyThread
thread
}
}
在上面代碼中的process方法被稱為回調函數
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27477.html