package com.test.sjcd;
public class Data {
private int value;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
package com.test.sjcd;
public class SubThread extends Thread{
private Data data;
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
public SubThread()
{
}
public SubThread(Data data)
{
this.data=data;
}
public void run()
{
System.out.println("子线程内:"+data.getValue());
this.data.setValue(200); //将值做修改
Test.callback(); //调用回调函数
}
}
package com.test.sjcd;
public class Test {
static boolean flag=false;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Data data=new Data();
data.setValue(20);
System.out.println("调用子线程前,值为"+data.getValue());
//第一种传值方式 通过构造函数传值
//SubThread subThread=new SubThread(data);
SubThread subThread=new SubThread();
//第二种传值方式 通过属性赋值
subThread.setData(data);
subThread.start();
//第三种方式 使用回调函数
while(!flag);
System.out.println("调用子线程后,值为"+data.getValue());
}
public static void callback()
{
flag=true;
}
}