1.回调接口
package havereturn;
public interface ICallBack
{
public void output(String str);
}
2.子线程
package havereturn;
import java.text.SimpleDateFormat;
public class Son extends Thread
{
private ICallBack event;
public Son(ICallBack callback)
{
event = callback;
}
public void run()
{
try
{
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
while (true)
{
Thread.sleep(3000);
event.output(fmt.format(new java.util.Date()));
Thread.sleep(3000);
}
}
catch (Exception e)
{}
}
}
3. 父线程
package havereturn;
public class Parent implements ICallBack
{
public static void main(String[] args)
{
Parent parent = new Parent();
Thread son = new Son(parent);
son.start();
}
public void output(String str)
{
System.out.println(str);
}
}
java线程 返回值-回调
最新推荐文章于 2023-03-10 17:06:10 发布