首页 \ 问答 \ 创建线程时出现IllegalThreadStateException(IllegalThreadStateException when creating thread)

创建线程时出现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
更新时间:2023-02-20 21:02

最满意答案

您需要在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( ...
  • 你不能。 与范围之外的函数通信的唯一方法是将对象放在全局范围内的某个位置。 相反,你应该在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 ...
  • 这些函数存储在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 ...
  • 立即调用的函数通常用于创建一个局部函数作用域,该作用域是私有的,不能从外部世界访问,并且可以定义它自己的局部符号而不影响外部世界。 这通常是一种很好的做法,但在这种特殊情况下,除了多行代码之外,我没有看到它带来任何好处,因为它不用于任何事情。 这段代码: (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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。