Scala 閉包
函數在變量不處於其有效作用域時,還能對變量進行訪問。下面舉例說明:
// 定義函數,它的返回值是一個匿名函數
def getFunc(msg : String) = (name : String) => println(msg + "," + name)
// 定義兩個函數變量
val fuc1 = getFunc("hello")
val fuc2 = getFunc("hi")
// 運行
fuc1("world")
fuc2("java")
如上所示,getFunc它傳入了兩個不同的msg,並返回了兩個不同的變量,這裏msg只是一個臨時變量,但是在getFunc創建完之後,還可以繼續保存在創建的函數變量fuc1和fuc2中。調用fuc1(“world”),值爲"hello"的msg被保留在了函數內部,也反覆使用,已經超出了它的作用域,這種情況就是閉包。
其實Scala通過爲每個函數創建一個對象來實現閉包,對於getFunc函數創建的函數,msg作爲函數對象存在,不同的函數擁有不同的msg。
其實在Spark源碼中有多處使用到了閉包,典型的比如在RDD算子函數中,map、flatMap等,有一個clean(f)的操作,就是爲了清除閉包。如果算子函數需要使用外部變量,那麼Driver端會將數據拷貝到每個task節點上,對於大變量可以使用broadcast。
Java閉包
在Java中,閉包一般使用 接口 + 內部類實現,內部類也可以是匿名內部類。在Java8中,有了Lambda表達式,它實際上也是匿名類。
特點
- 不能訪問封裝類的非final成員
如果有一個匿名內部類有一個成員變量與外部成員名稱相同,那麼內部內會覆蓋外部成員變量,這種情況下,外部成員在匿名內部類是不可見的,也不能使用this關鍵字來訪問。
public void test() {
String str = "string in test";
new Thread(
new Runnable(){
String str = "string in runnable";
public void run() {
String str = "string in run";
System.out.println(str);
System.out.println(this.str);
}
}
).start();
}
// 輸出結果
string in run
string in runnable
- functional interfaces
一個只有一個方法的接口。大多數的回調接口都是functional interfaces,比如runnable、callable等 - Lambda表達式
Lambda表達式實際上也是匿名類,它看起來更像是方法,我們用它來實現閉包。
public class closure {
String str = "string in closure";
public static void main(String[] args){
new closure().test();
}
public void test() {
String str = "string in test";
new Thread(
() -> {
System.out.println(str);
System.out.println(this.str);
}
).start();
}
}
// 運行結果
string in test
string in closure
Lambda表達式解決了匿名內部類訪問外部變量的可見性問題,它不允許創建覆蓋變量,如果定義那麼會直接報錯。
5. 閉包可能出現的問題
會導致變量的生命週期變長;閉包間共享變量使用final關鍵字。
如下所示:
public class DefaultMethod {
public static void main(String[] args){
MyClass my = new MyClass();
It it = my.subNum();
it.sub();
it.sub();
my = null;
System.out.println("myclass is null");
it.sub();
it.sub();
}
}
interface It {
void sub();
}
class MyClass {
public int num = 10;
public MyClass() {
System.out.println("init num = " + num);
}
public It subNum() {
return () -> {
num--;
System.out.println("num = " + num);
};
}
}
init num = 10
num = 9
num = 8
myclass is null
num = 7
num = 6
MyCalss對象已經爲null,但是it依然可以使用。