[英]Spring Boot shared thread
I am developing my Spring boot application wich gets two requests: /start and /stop. 我正在开发我的Spring引导应用程序,它有两个请求:/ start和/ stop。 I need to create one shared thread for all clients requests.
我需要为所有客户端请求创建一个共享线程。
When the first request "/start" will be received from client, app will create one thread shared by local variable T1. 当从客户端收到第一个请求“ / start”时,app将创建一个由本地变量T1共享的线程。
When the second request "/stop" will be received, app will set boolean variable of thread "stopped" to stop it and the thread should stop. 当收到第二个请求“ / stop”时,应用程序将设置线程“ stopped”的布尔变量以将其停止,线程应停止。
Is next code provides safe for this shared thread? 下一个代码是否为此共享线程提供了安全? Should i use the local variable for thread object or need to do it by another way?
我应该将局部变量用于线程对象还是需要通过其他方式来实现?
package com.direct.webflow;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@EnableAutoConfiguration
@Controller
public class WebApp {
ThreadDemo T1;
@RequestMapping("/start")
@ResponseBody
String start() {
synchronized(this){
if (T1 == null || T1.stopped) {
T1= new ThreadDemo( "Thread-1");
T1.start();
} else {
return "Already started!";
}
}
return "Thread started!";
}
@RequestMapping("/stop")
@ResponseBody
String end() {
if (T1 == null) {
System.out.println("Not started!");
return "Not started!";
} else if (!T1.stopped) {
T1.stopped=true;
System.out.println("Trying to stop!");
return "Stopped!";
} else {
return "Already stopped!";
}
}
public static void main(String[] args) throws Exception {
SpringApplication.run(WebApp.class, args);
}
}
package com.direct.webflow;
public class ThreadDemo extends Thread {
private Thread t;
private String threadName;
public volatile boolean stopped=false;
ThreadDemo(String name){
threadName = name;
System.out.println("Creating " + threadName );
}
public void run() {
int i=0;
System.out.println("Running " + threadName );
while (!stopped) {
System.out.println("Thread: " +this.isInterrupted()+ threadName + ", " + i++);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Thread: STOP!");
break;
}
}
System.out.println("Thread " + threadName + " exiting.");
}
public void start ()
{
stopped=false;
System.out.println("Starting " + threadName );
if (t == null)
{
t = new Thread (this, threadName);
t.start ();
}
}
}
This is very close. 这非常接近。 You need to add the synchronized(this) block in your controller end() method.
您需要在控制器end()方法中添加synced(this)块。 Otherwise you may have a race condition if /stop and /start are being called simultaneously.
否则,如果同时调用/ stop和/ start,则可能会出现竞争状况。
Since Spring controllers are singletons you are OK to use a member variable like you have done here. 由于Spring控制器是单例的,因此可以像在此一样使用成员变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.