运行单独线程的结果不清楚

时间:2018-09-18 21:39:20

标签: multithreading scala java-threads

我有一些我认为很简单的东西,但是后来我思考了一段时间后遇到了一个我不明白的问题。

这是涉及两个线程的简单的最小代码段:

package com.alexander.arendar

package object concscalaprog {

  def thread(block: =>Unit):Thread = {
    val t = new Thread{
      override def run(): Unit = block
    }
    t.run()
    t
  }
}

package com.alexander.arendar.concscalaprog

object Chapter2 {

  def main(args:Array[String]):Unit = {
    val t1 = thread(println(Thread.currentThread().getName))
    t1.join()
  }

}

我希望在控制台中看到与

不同的内容
  

主要

但是这正是我在程序输出中看到的。 你们可以帮助我理解为什么它不打印出它创建的单独线程的名称吗?

1 个答案:

答案 0 :(得分:1)

此代码段

java.io.IOException: Unable to parse response body for Response{requestLine=POST /_mget HTTP/1.1, host=http://elasticsearch:9200, response=HTTP/1.1 200 OK}
at org.elasticsearch.client.RestHighLevelClient$1.onSuccess(RestHighLevelClient.java:582) ~[elasticsearch-rest-high-level-client-6.3.2.jar!/:6.3.2]
at org.elasticsearch.client.RestClient$FailureTrackingResponseListener.onSuccess(RestClient.java:621) [elasticsearch-rest-client-6.3.2.jar!/:6.3.2]
at org.elasticsearch.client.RestClient$1.completed(RestClient.java:375) [elasticsearch-rest-client-6.3.2.jar!/:6.3.2]
at org.elasticsearch.client.RestClient$1.completed(RestClient.java:366) [elasticsearch-rest-client-6.3.2.jar!/:6.3.2]
at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:123) [httpcore-4.4.10.jar!/:4.4.10]
at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.responseCompleted(DefaultClientExchangeHandlerImpl.java:181) [httpasyncclient-4.1.4.jar!/:4.1.4]
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.processResponse(HttpAsyncRequestExecutor.java:442) [httpcore-nio-4.4.10.jar!/:4.4.10]
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.inputReady(HttpAsyncRequestExecutor.java:332) [httpcore-nio-4.4.10.jar!/:4.4.10]
at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:265) [httpcore-nio-4.4.10.jar!/:4.4.10]
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81) [httpasyncclient-4.1.4.jar!/:4.1.4]
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39) [httpasyncclient-4.1.4.jar!/:4.1.4]
at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:114) [httpcore-nio-4.4.10.jar!/:4.4.10]
at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162) [httpcore-nio-4.4.10.jar!/:4.4.10]
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337) [httpcore-nio-4.4.10.jar!/:4.4.10]
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315) [httpcore-nio-4.4.10.jar!/:4.4.10]
at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276) [httpcore-nio-4.4.10.jar!/:4.4.10]
at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104) [httpcore-nio-4.4.10.jar!/:4.4.10]
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:588) [httpcore-nio-4.4.10.jar!/:4.4.10]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]
Caused by: java.lang.NullPointerException: null
    at some.random.package.elasticsearch.ElasticsearchService$1.lambda$onResponse$0(ElasticsearchService.java:81) ~[classes!/:na]
    at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_181]
    at some.random.package.elasticsearch.ElasticsearchService$1.onResponse(ElasticsearchService.java:78) ~[classes!/:na]
    at some.random.package.elasticsearch.ElasticsearchService$1.onResponse(ElasticsearchService.java:75) ~[classes!/:na]
    at org.elasticsearch.client.RestHighLevelClient$1.onSuccess(RestHighLevelClient.java:580) ~[elasticsearch-rest-high-level-client-6.3.2.jar!/:6.3.2]
    ... 18 common frames omitted

使用重写的方法val t = new Thread{ override def run(): Unit = block } t.run() 定义类型t的变量Thread,然后立即在同一线程上调用run 。 >

这与写作没什么不同

run

abstract class Q { def bar(): Unit } val q = new Q { def bar(): Unit = println(Thread.currentThread.getName) } q.bar() t类型而Threadq类型的事实是不相关的:Qrun都被执行在主线程上,使用相同的堆栈。

只需将bar替换为run()

下面是一个简短的示例来演示它:

start()

给出lazy val t: Thread = new Thread { override def run(): Unit = { println(Thread.currentThread == t) } } t.run() ,而

false

给予lazy val t: Thread = new Thread { override def run(): Unit = { println(Thread.currentThread == t) } } t.start()