Details
-
Improvement
-
Status: Resolved
-
Major
-
Resolution: Won't Fix
-
2.0.0
-
None
-
Important
Description
I think the singleton pattern of SparkContext is inefficient if there are many request to get the SparkContext,
So we can write the singleton pattern as below,The second way if more efficient when there are many request to get the SparkContext.
// the current version def getOrCreate(): SparkContext = { SPARK_CONTEXT_CONSTRUCTOR_LOCK.synchronized { if (activeContext.get() == null) { setActiveContext(new SparkContext(), allowMultipleContexts = false) } activeContext.get() } } // by myself def getOrCreate(): SparkContext = { if (activeContext.get() == null) { SPARK_CONTEXT_CONSTRUCTOR_LOCK.synchronized { if(activeContext.get == null) { @volatile val sparkContext = new SparkContext() setActiveContext(sparkContext, allowMultipleContexts = false) } } activeContext.get() } }