回调函数
有两个类,A,B,在类A中调用B,在B中调用A的方法完成A的工作,那么这个在B类中调用的A的函数就称为回调函数。
异步回掉函数:类A将自己的工作交给类B后,继续执行剩下的程序,而B继续完成A交给的工作。
使用方法:
1、定义一个接口
2、A可以直接继承此接口,也可以定义一个内部类继承此接口;
定义一个方法,调用B中的方法
3、B中的方法调用A中的方法。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
//定义接口 public interface doJob { public void fillBlank(int a,int b,int result) throws InterruptedException; } //定义A类 public class Student { private String name = null; public Student(String name){ this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return name; } @SuppressWarnings("unused") private int calcAdd(int a,int b){ return a+b; } private int useCalculator(int a,int b){ return new Calculator().add(a,b); } public void fillBlank(int a,int b){ int result = useCalculator(a,b); System.out.println(name + "使用计算器:" + a + " + " + b + " = " + result); } public void fillBlank(int result){ System.out.println(name + "使用计算器: a + b = " + result); } public class doHomeWork implements doJob{ public void fillBlank(int a, int b, int result) throws InterruptedException { System.out.println(name + "求助小红计算:" +a+" + "+b+" = "+result); } } public void callHelp(final int a,final int b) throws InterruptedException { new Thread(new Runnable() { public void run() { try { Thread.sleep(5000); new SuperCalculator().add(a,b,new doHomeWork()); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } } //定义B类 public class SuperCalculator { public void add(int a,int b,doJob s) throws InterruptedException { int result = a +b; s.fillBlank(a,b,result); } } //主函数,测试 public class Test { public static void main(String[] args) throws InterruptedException { int a =1; int b = 2; Student s = new Student("小明"); s.callHelp(a,b); System.out.println("结束"); } }
输出结果:
结束
小明求助小红计算:1 + 2 = 3
线程的三种方式
Runnable
实现Runnable接口,需要实现run方法,run方法中可以调用其他方法,使用其他类,并声明变量,就像主线程一样,代码如下:
public class RunableProcessor implements Runnable {
private Thread t;
private String threadName;
public RunableProcessor(String threadName) {
this.threadName = threadName;
}
public void run() {
System.out.println("ssss");
}
public void start(){
t = new Thread(this,threadName);
t.start();
}
}
//调用
public class TestThread {
public static void main(String args[]) {
RunnableProcessor R1 = new RunnableProcessor( "Thread-1");
R1.start();
}
}
Thread
继承Thread类,run方法中可以调用其他方法,使用其他类,并声明变量,就像主线程一样,代码如下:
public class ThreadProcessor extends Thread {
private String name = "sss";
private Thread t;
@Override
public synchronized void start() {
t = new Thread(this,name);
t.start();
}
@Override
public void run() {
System.out.println(name);
}
}
//调用
public class TestThread {
public static void main(String args[]) {
ThreadProcessorR1 = new ThreadProcessor( "Thread-1");
R1.start();
}
}
Thread类本身有一些函数,比如修改进程名字,修改优先级。
Callable方式
此方式有返回值,需要继承Callable接口实现call方法。to那个锅FutureTask来完成调用。
//实现Callable接口
public class CallableProcessor implements Callable<String> {
private String str = "s";
public String call() throws Exception {
String result = "";
for(int i =0;i<20;i++){
result.concat(str);
result += str;
}
return result;
}
}
//调用,并获取返回值
public class Test {
public static void main(String[] args) {
CallableProcessor callableProcessor = new CallableProcessor();
FutureTask<String> ft = new FutureTask<String>(callableProcessor);
Thread t = new Thread(ft);
t.start();
try {
System.out.println(ft.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.exit(0);
}
}