【问题标题】:Spring Boot shared threadSpring Boot 共享线程
【发布时间】:2016-06-22 08:35:52
【问题描述】:

我正在开发我的 Spring Boot 应用程序 收到两个请求:/start 和 /stop。 我需要为所有客户端请求创建一个共享线程。

当从客户端收到第一个请求“/start”时,应用程序将创建一个由局部变量T1共享的线程。

当收到第二个请求“/stop”时,应用程序将设置线程“stopped”的布尔变量来停止它,线程应该停止。

下一个代码是否为这个共享线程提供安全? 我应该将局部变量用于线程对象还是需要 换一种方式?

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 ();
          }
       }

    }

【问题讨论】:

  • 你不应该首先在容器中启动/停止线程。

标签: java multithreading spring-boot


【解决方案1】:

这非常接近。您需要在控制器的 end() 方法中添加 synchronized(this) 块。否则,如果同时调用 /stop 和 /start,您可能会遇到竞争条件。

由于 Spring 控制器是单例的,因此您可以像在此处所做的那样使用成员变量。

【讨论】:

    猜你喜欢
    • 2017-05-05
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 2017-07-17
    • 2018-05-08
    相关资源
    最近更新 更多