[英]Run java program with thread
Good evening, I got this two programs here 晚上好,我在这里得到了这两个节目
httpServer.java 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();
}
}
}
Test.java Test.java
public class Test {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Now i want that the Test.java to start working when i start the httpServer.java. 现在,我希望在启动httpServer.java时Test.java开始工作。 I want to achieve this with threads.
我想用线程来实现。 I found this here online which expalins how i make a thread but i dont know how to get the Test.java to work there.
我在网上找到了这个,它说明了我如何创建线程,但是我不知道如何使Test.java在那里工作。
Note: I know i could write both in one programm but i want to know how to work with threads for another project im working on. 注意:我知道我可以在一个程序中同时编写两个程序,但是我想知道如何为正在处理的另一个项目使用线程。
To start a thread you need to implement the run
-Method. 要启动线程,您需要实现
run
-Method。 Everything inside the run
-Method will be executed in a new Thread. run
-Method内部的所有内容都将在新线程中执行。
You did not implement a run method so the call of server.start();
您没有实现run方法,因此调用
server.start();
does acutally nothing. 一点也不做。 With a
run
-Method it would look like this: run
-Method会看起来像这样:
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.