IllegalArgumentException

IllegalArgumentException

本文介绍了Thread上的Clojure错误:java.lang.IllegalArgumentException:Key必须是整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对2件事感到困惑。一,错误消息本身。和两个,事实,这个代码旋转了这么多的线程。我认为它会旋转2或3线程响应一个单一的请求,但是当我在localhost上运行这个并指向我的浏览器(只是一次),它旋转了40个线程。诚然,我使用Chrome,它在后台发送3个请求,出于各种复杂的原因,谷歌捍卫作为一个功能(速度)。

I am puzzled by 2 things. One, the error message itself. And two, the fact that this code spins up so many threads. I was thinking it would spin up 2 or 3 threads in response to a single request, but when I run this on localhost and point my browser at it (just once) it spins up 40 threads. Admittedly, I was using Chrome, which sends 3 requests in the background, for various complicated reasons that Google defends as a "feature" (speed).

这个错误是什么意思?

Exception in thread "Thread-39" java.lang.IllegalArgumentException: Key must be integer
at clojure.lang.APersistentVector.invoke(APersistentVector.java:250)
at serve_pages_from_memory.core$listen_and_respond$fn__51.invoke(core.clj:30)
at clojure.lang.AFn.run(AFn.java:24)
at java.lang.Thread.run(Thread.java:680)

我看到类似的错误已经讨论过StackOverflow以前,但我不知道他们如何绑定到我的问题:

I see similar errors have been discussed on StackOverflow before, but I don't see how they tie in to my problem:

我试图构建一个简单的应用程序来服务一个页面。我这样做主要是为了测试我自己的Clojure知识。这里是大部分代码:

I'm trying to build a simple app that serves a page. I do this mostly to test my own knowledge of Clojure. Here is most of the code:

(defn start-thread [f]
(doto (new Thread f) (.start)))

(defn serve-page [wrt]
(let [content-length (count page-string)]
(. wrt println "HTTP/1.1 200 OK")
(. wrt println "Content-Type: text/html; charset=UTF-8")
(. wrt println "Connection: Keep-Alive")
(. wrt println (str "Content-Length: " content-length))
(. wrt print "\r\n")
(. wrt print page-string)
(. wrt flush)
(. wrt close)))

(defn listen-and-respond [ss]
(let [client (. ss accept)]
(start-thread #([client]
                  (let [wrt (new PrintWriter (new BufferedWriter (new OutputStreamWriter (. client (getOutputStream)))))]
                    (serve-page wrt))))))

(defn create-server [port]
(let [ss (new ServerSocket port)]
(start-thread #(when-not (. ss (isClosed))
                 (try (listen-and-respond ss)
                      (catch SocketException e))
                 (recur)))
ss))

(defn -main [& args]
(println "Server is starting")
(let [port (Integer/parseInt (first args))]
(create-server port)))

错误似乎是抱怨这行:

                (serve-page wrt))))))

函数名和PrintWriter对象。错误消息将建议这是关于地图与键。这个错误没有意义。

But that is a function name and a PrintWriter object. The error message would suggest this was something about a map with keys. The error makes no sense.

有任何建议吗?

推荐答案

#([client]
   (let [wrt (new PrintWriter (new BufferedWriter (new OutputStreamWriter (. client (getOutputStream)))))]
     (serve-page wrt)))

是无稽之谈。如果您想要一个带有命名参数的lambda,请使用 fn 形式:

is nonsense. If you want a lambda with named parameters, use the fn form:

(fn [client]
  (let [wrt (new PrintWriter (new BufferedWriter (new OutputStreamWriter (. client (getOutputStream)))))]
    (serve-page wrt)))

这篇关于Thread上的Clojure错误:java.lang.IllegalArgumentException:Key必须是整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 05:46