Github user zsxwing commented on a diff in the pull request:

    https://github.com/apache/spark/pull/8781#discussion_r39734615
  
    --- Diff: core/src/main/scala/org/apache/spark/util/ThreadUtils.scala ---
    @@ -86,4 +87,60 @@ private[spark] object ThreadUtils {
         val threadFactory = new 
ThreadFactoryBuilder().setDaemon(true).setNameFormat(threadName).build()
         Executors.newSingleThreadScheduledExecutor(threadFactory)
       }
    +
    +  /**
    +   * Run a piece of code in a new thread, and the get result. Exception in 
the new thread is
    +   * thrown in the caller thread with an adjusted stack trace that removes 
references to this
    +   * method for clarity. The exception stack traces will be like the 
following
    +   *
    +   * SomeException: exception-message
    +   *   at CallerClass.body-method (sourcefile.scala)
    +   *   at ... run in separate thread using 
org.apache.spark.util.ThreadUtils ... ()
    +   *   at CallerClass.caller-method (sourcefile.scala)
    +   *   ...
    +   */
    +  def runInNewThread[T](
    +      threadName: String,
    +      isDaemon: Boolean = true)(body: => T): T = {
    +    @volatile var exception: Option[Throwable] = None
    +    @volatile var result: T = null.asInstanceOf[T]
    +
    +    val thread = new Thread(threadName) {
    +      override def run(): Unit = {
    +        try {
    +          result = body
    +        } catch {
    +          case NonFatal(e) =>
    +            exception = Some(e)
    +        }
    +      }
    +    }
    +    thread.setDaemon(isDaemon)
    +    thread.start()
    +    thread.join()
    +
    +    exception match {
    +      case Some(realException) =>
    +        // Remove the part of the stack that shows method calls into this 
helper method
    +        val baseStackTrace = 
Thread.currentThread().getStackTrace().dropWhile(
    +          ! _.getClassName.contains(this.getClass.getSimpleName)).drop(1)
    --- End diff --
    
    nit: could you add a comment about the magic number `1`, such as `remove 
"java.lang.Thread.getStackTrace"`? 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to