1 package com.laurdawn; 2 3 public class Test { 4 5 public static void main(String[] args) { 6 Callbak callback = new Callbak() { 7 8 @Override 9 public void getPosition(float x) { 10 System.out.println("接口回调的位置:" + x); 11 } 12 }; 13 new Mythread(callback).start(); 14 } 15 16 public static class Mythread extends Thread { 17 private Callbak callback; 18 private float currentX = 0; 19 20 public Mythread(Callbak callback) { 21 this.callback = callback; 22 } 23 24 @Override 25 public void run() { 26 while (true) { 27 currentX++; 28 callback.getPosition(currentX); 29 try { 30 sleep(300); 31 } catch (InterruptedException e) { 32 e.printStackTrace(); 33 } 34 } 35 } 36 } 37 38 public interface Callbak { 39 public void getPosition(float x); 40 } 41 42 }