创建线程时出现IllegalThreadStateException(IllegalThreadStateException when creating thread)
所以这里的代码应该是我错误的来源:
try { config = new Configuration(configFile); } catch (ConfigurationException e1) {} try { // Create a ServerSocket ss = new ServerSocket(PORT); System.out.println("Server bound at port " + ss.getLocalPort()); }catch (Exception e) {} new ServerThread(ss, config).start();
这是我第一次创建线程,因此线程无法运行。 一旦创建了这个线程,我也没有再次引用它,所以我不确定如何获得IllegalThreadStateException。
这是堆栈跟踪:
Exception in thread "Thread-3" java.lang.IllegalThreadStateException at java.lang.Thread.start(Unknown Source) at HttpServer.<init>(HttpServer.java:36)
任何帮助将不胜感激。
编辑:将线程启动代码更改为:
ServerThread initServerThread = new ServerThread(ss, config); System.out.println(initServerThread.getState().toString()); initServerThread.start();
线程状态为“RUNNABLE”
另外这里是线程类......
public class ServerThread extends Thread { ServerSocket serverSocket; Socket nextClient; Configuration config; public ServerThread(ServerSocket ss, Configuration newConfig) { super(); serverSocket = ss; config = newConfig; start(); } public void run() { try { nextClient = serverSocket.accept(); } catch (IOException e) { System.err.println(e); System.err.println("Usage: java HttpRequest <port>"); } // Create new thread to listen for next incoming connection. new ServerThread(serverSocket, config).start(); try { nextClient = serverSocket.accept(); } catch (IOException e) {} HttpRequestHandler httpHandler = new HttpRequestHandler(nextClient, config); httpHandler.parseHttpRequest(); } }
So here's the code that should be the source of my error:
try { config = new Configuration(configFile); } catch (ConfigurationException e1) {} try { // Create a ServerSocket ss = new ServerSocket(PORT); System.out.println("Server bound at port " + ss.getLocalPort()); }catch (Exception e) {} new ServerThread(ss, config).start();
This is the first time I'm creating a thread so the thread can't already be running. I'm also not referencing this thread again once it has been created so I'm not sure how I could be getting an IllegalThreadStateException.
Here's the stack trace:
Exception in thread "Thread-3" java.lang.IllegalThreadStateException at java.lang.Thread.start(Unknown Source) at HttpServer.<init>(HttpServer.java:36)
Any help would be GREATLY appreciated.
EDIT: Changed the thread starting code to this:
ServerThread initServerThread = new ServerThread(ss, config); System.out.println(initServerThread.getState().toString()); initServerThread.start();
The thread state is "RUNNABLE"
Also here's the thread class...
public class ServerThread extends Thread { ServerSocket serverSocket; Socket nextClient; Configuration config; public ServerThread(ServerSocket ss, Configuration newConfig) { super(); serverSocket = ss; config = newConfig; start(); } public void run() { try { nextClient = serverSocket.accept(); } catch (IOException e) { System.err.println(e); System.err.println("Usage: java HttpRequest <port>"); } // Create new thread to listen for next incoming connection. new ServerThread(serverSocket, config).start(); try { nextClient = serverSocket.accept(); } catch (IOException e) {} HttpRequestHandler httpHandler = new HttpRequestHandler(nextClient, config); httpHandler.parseHttpRequest(); } }
原文:https://stackoverflow.com/questions/20314849
最满意答案
您需要在IIFE中返回一个功能。 如果您的IIF不是微不足道并且具有许多功能,您还可以考虑使用Reveal Module Pattern 。
var build = (function() { var f = function() { console.log('hello'); }; f(); return f; })(); function test() { build(); } test();
You need to return a function in your IIFE. If you IIF is not trivial and has many functionalities you could also consider using Reveal Module Pattern.
var build = (function() { var f = function() { console.log('hello'); }; f(); return f; })(); function test() { build(); } test();
相关问答
更多-
你的初始例子不值得在匿名函数中执行,所以它是一个很好的例子来理解为什么要使用这种技术。 这是探索状态捕获的一个很好的例子: var list = [{id: 1, data: null}, ...]; for (var i = 0; i < list.length; i++) { (function(item) { // start async request, for each item in the list, in the order of the list asyncAjax( ...
-
如何将jQuery.ready与其他匿名自执行函数一起使用?(How to use jQuery.ready with other anonymous self executing functions?)[2022-05-04]
你不能。 与范围之外的函数通信的唯一方法是将对象放在全局范围内的某个位置。 相反,你应该在IIFE中调用$(document).ready() ,这样它就能通过闭包访问局部变量。 You can't. The only way to communicate with a function outside your scope is to put an object somewhere in the global scope. Instead, you should call $(document).read ... -
不,不会在定义上调用匿名函数。 你正在看的只是一个函数表达式作为第一个参数传递给before函数。 然后, before函数将调用此函数作为稍后测试运行的一部分。 “设置用户数据”测试将在函数传递到完成before运行, 这是因为测试框架在每次测试“之前”调用before函数。 请注意, before它只是一个由测试框架自行提供的函数,它没有特殊的语言功能。 No, anonymous functions are not called on definition. What you are looking ...
-
Javascript匿名函数(Javascript anonymous functions)[2023-03-28]
这些函数存储在jQuery.cache 。 只要您使用.empty()等jQuery方法,就会清除所有受影响元素的数据。 如果你做了这样的事情: document.getElementById('container').innerHTML = ''; 然后你可能会有一个非常讨厌的内存泄漏,因为每个元素和jQuery.cache之间的连接将被切断,并且数据(可能更多)将在缓存中孤立。 删除/覆盖内容时坚持使用jQuery方法,你会没事的。 即使这样做: $('#container').html(''); ... -
您需要在IIFE中返回一个功能。 如果您的IIF不是微不足道并且具有许多功能,您还可以考虑使用Reveal Module Pattern 。 var build = (function() { var f = function() { console.log('hello'); }; f(); return f; })(); function test() { build(); } test(); You need to return a functi ...
-
什么是自动执行的匿名函数,或者这个代码做了什么?(what is self-executing anonymous function or what is this code doing?)[2022-09-01]
立即调用的函数通常用于创建一个局部函数作用域,该作用域是私有的,不能从外部世界访问,并且可以定义它自己的局部符号而不影响外部世界。 这通常是一种很好的做法,但在这种特殊情况下,除了多行代码之外,我没有看到它带来任何好处,因为它不用于任何事情。 这段代码: (function(exports){ exports.notGlobalFunction = function() { console.log('I am not global'); }; }(module)); 如果没有像这样 ... -
main ()内部或外部的()的位置丝毫无关紧要。 (更多) 在这个问题上的更多讨论,但是这个问题并没有涉及你提出的call期权。 call需要至少一个根据规范的参数,所以要与前两个选项基本相同,您需要: (function() { }).call(undefined); ...确保某些实现不会因为不提供参数而对你产生不满。 The location of the () inside or outside the main () doesn't matter in the slightest. (Much ...
-
(function() { this.Lib = {}; }).call(this); 定义它所调用的Object的Lib属性,并立即调用它,通常是window 。 它可以改为引用拥有调用它的方法的Object。 (function() { var Lib = {}; window.Lib = Lib; })(); 定义window的Lib属性,无论它在何处被调用(尽管它也被立即调用)。 (function(global) { var Lib = {}; global.Lib = ...
-
JavaScript中的函数是第一类成员 ,这意味着您可以像使用该语言中的任何其他数据类型一样使用它们 - 您可以将它们作为参数传递,将它们作为其他函数内的成员变量传递,从函数等返回它们。 在你问到的情况下,它就像任何其他命名变量一样工作,并且可以像下面那样调用该函数,因为它是一个函数: function myFunc(anonymous){ var arg1; anonymous(arg1); } myFunc(function(arg1){console.log(arg1)}); F ...
-
提供的代码并不多,但错误可能是由于您在匿名函数中引用this错误。 this.loadJson(JSON.stringify(dataSnapshot.val())); 尝试将其存储在匿名函数之外,然后在匿名函数中使用它,就像这样 loadGesturesFromDatabase: function () { var firebaseRef = new Firebase("https://test.firebaseio.com/"); //We keep a reference to the co ...