java - 使用线程运行java程序

标签 java multithreading server

晚上好,我这里有这两个程序

httpServer.java

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.concurrent.atomic.AtomicInteger;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class httpServer extends Thread  {



public static void main(String[] args) throws Exception {
    HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
    server.createContext("/test", new MyHandler());
    server.setExecutor(null); // creates a default executor
    server.start();


}

static class MyHandler implements HttpHandler {
    AtomicInteger atomicInteger = new AtomicInteger(0); 
    int theValue = atomicInteger.get(); 
    @Override
    public void handle(final HttpExchange t) throws IOException {
        final String response;

        final String requestMethod = t.getRequestMethod();
        if ("GET".equals(requestMethod)) {
            response = String.format("Besuche: %d%n", atomicInteger.addAndGet(1));
        }
        else if ("POST".equals(requestMethod)) {
            atomicInteger.set(0);

            response = "Reset to 0";
        }
        else {
            throw new IOException("Unsupported method");
        }

        t.sendResponseHeaders(200, response.length());
        final OutputStream os = t.getResponseBody();
        os.write(response.getBytes());
        os.close();
    }
}



}

测试.java

public class Test {
public static void main(String[] args) {
    System.out.println("Hello World!"); 
}
}

现在我希望 Test.java 在我启动 httpServer.java 时开始工作。 我想用线程来实现这一点。我找到了这个here在线解释了我如何创建线程,但我不知道如何让 Test.java 在那里工作。

注意:我知道我可以在一个程序中编写这两个代码,但我想知道如何使用我正在处理的另一个项目的线程。

最佳答案

要启动线程,您需要实现run方法。 run 方法内的所有内容都将在新线程中执行。

您没有实现 run 方法,因此调用 server.start(); 实际上什么也不做。使用run-方法,它看起来像这样:

public class httpServer extends Thread  
{
    //Everything inside this method is executed in a new Thread
    @Override
    public void run()
    {
        super.run();

        System.out.println("THIS IS EXECUTED IN A THREAD");

        this.serverStuff();
    }

    private void serverStuff()
    {
        HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
        server.createContext("/test", new MyHandler());
        server.setExecutor(null); // creates a default executor
    }
}

public class Test 
{
    public static void main(String[] args) 
    {
        System.out.println("THIS IN NOT EXECUTED IN THREAD");

        //This call creates a new Thread. It calls the run()-Method
        new httpServer().start();
    }
}

关于java - 使用线程运行java程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33398050/

相关文章:

multithreading - 在基于 SQLite 的 Delphi 应用程序中是否需要使用线程?

java - Java中RandomAccessFile的并发

java - 我如何更改此设置以用 html 页面替换 404 页面错误?

database - 什么是COB环境?

java - 在哪里复制 apache tomcat 服务器中的 servlet 文件才能与 Web 正常工作?

java - 检查整数是否在java中有一些字符内容

java - 计算 FilteredList 中项目(元素)的平均值

java - 如何设置JInternalFrame的大小

asp.net - 客户端可以被视为一个线程吗?

php - 这是黑客入侵的迹象吗?