Spring Boot Shared ThreadJAVA

Программисты JAVA общаются здесь
Anonymous
Spring Boot Shared Thread

Сообщение Anonymous »

Я разрабатываю свое приложение Spring Boot, которое
получает два запроса: /start < /code> и /stop.
Мне нужно создать один общий поток для всех запросов клиентов. Установлена ли логическая переменная потока «остановлен», чтобы остановить его, и поток должен остановиться. < /p>
Предоставляется ли следующий код для этого общего потока?
Должен ли я использовать локальную переменную для объекта потока или нужно
сделать это другим способом? < /p>
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 ();
}
}
}


Подробнее здесь: https://stackoverflow.com/questions/358 ... red-thread

Вернуться в «JAVA»