異步回調函數-建立進程的三種方式

回調函數java

有兩個類,A,B,在類A中調用B,在B中調用A的方法完成A的工做,那麼這個在B類中調用的A的函數就稱爲回調函數。web

異步回掉函數:類A將本身的工做交給類B後,繼續執行剩下的程序,而B繼續完成A交給的工做。異步

 

使用方法:ide

一、定義一個接口函數

二、A能夠直接繼承此接口,也能夠定義一個內部類繼承此接口;測試

  定義一個方法,調用B中的方法this

三、B中的方法調用A中的方法。spa

//定義接口
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("結束");
    }
}
View Code

輸出結果:
結束
小明求助小紅計算:1 + 2 = 3線程

線程的三種方式code

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);
    }
}
相關文章
相關標籤/搜索