如何中断某个套接字IO操作阻塞的线程而不关闭它(How to interupt a thread blocked by some socket IO operation without closing it)
我真的很想知道如何中断一个被某些I / O操作阻塞的线程? I / O是通过套接字,我不想
close()
套接字。我尝试使用
Thread.interrupt()
来中断该线程,但没有成功。线程被IO操作阻塞:
void run(){ byte[] data=new byte[1024]; in.read(data);//blocked! }
线程中断被阻塞的线程(没有工作):
void run(){ blockedThread.interrupt();//no success! }
I am really wondered how to interrupt a thread which is blocked by some I/O operation? The I/O is through a socket, and I do not want to
close()
the socket.I tried using
Thread.interrupt()
to interrupt the thread, but without success.Thread blocked by IO operation:
void run(){ byte[] data=new byte[1024]; in.read(data);//blocked! }
Thread to interrupt the blocked thread (did not work):
void run(){ blockedThread.interrupt();//no success! }
原文:https://stackoverflow.com/questions/19435977
最满意答案
你所要做的就是像这样改变你的代码:
@RestController @RequestMapping("/ctrl") public class TestController { @GetMapping public void test() { } } @RestController @RequestMapping("/ctrl2") public class TestController2 { @GetMapping public void test() { } }
请注意,我已将
RequestMapping
注释添加到控制器。您在
@RestController
注释中使用的名称不必与映射@RestController
。 它用作将在Spring上下文中注册的bean的名称All you have to do is change your code like so:
@RestController @RequestMapping("/ctrl") public class TestController { @GetMapping public void test() { } } @RestController @RequestMapping("/ctrl2") public class TestController2 { @GetMapping public void test() { } }
Notice that I have added the
RequestMapping
annotation to the controllers.The name you where using in the
@RestController
annotation does not have to do with mapping. It's used as the name of the bean that will be registered in the Spring context
相关问答
更多-
有几个选项可以使用。 很好的方法是使用异常和类来处理调用@ControllerAdvice : @ControllerAdvice class GlobalControllerExceptionHandler { @ResponseStatus(HttpStatus.CONFLICT) // 409 @ExceptionHandler(DataIntegrityViolationException.class) public void handleConflict() { ...
-
让我们建议今后与用户相关的实体数量会增加。 所以显而易见的是根据实体分割它更好: UserController - > UserService - > UserRepository, ContactController - > ContactService - > ContactRepository, FriendshipController - > FriendshipService - > FriendshipRepository 根据我的经验,用户控制器 @RestController @Reques ...
-
Spring Boot - Rest Controller在热插拔后失败(Spring Boot - Rest Controller fails after hot swap)[2023-05-10]
要回答我的问题,可以通过将以下内容添加到application.properties中解决问题 ## Amount of time (in milliseconds) to wait between polling for classpath changes. spring.devtools.restart.poll-interval=3000 # Amount of quiet time (in milliseconds) required without any classpath changes b ... -
假设您的x包类似于com.example.mybootapp而您的主Application.class位于x包内,那么您需要添加此包 @SpringBootApplication @ComponentScan({"com.example.mybootapp","com.example.someother","one.more.pack"}) 在您的主Application.class方法或配置文件上。 @SpringBootApplication本身由@Configuration @EnableAutoC ...
-
Spring Boot Thymeleaf ViewResolver on Rest控制器(Spring Boot Thymeleaf ViewResolver on Rest controllers)[2022-07-16]
对不起大家,那是我的错。 以前我将应用程序从Spring MVC迁移到Spring Boot,并将其拆分为REST API和前端。 特别是该控制器没有注释为@RestController 。 固定。 Sorry everyone, that was my mistake. Previously i migrated the app from Spring MVC to Spring Boot with splitting it to REST API and front-end. Particularly ... -
Spring Boot REST映射冲突事件在不同的控制器中(Spring Boot REST mapping conflict event in different controller)[2022-06-03]
你所要做的就是像这样改变你的代码: @RestController @RequestMapping("/ctrl") public class TestController { @GetMapping public void test() { } } @RestController @RequestMapping("/ctrl2") public class TestController2 { @GetMapping public void test() { ... -
您正在变得复杂,为了启用文件上传,只需使用application.properties 属性正确配置即可。 multipart.enabled=true 并确保你的班级路径上有spring-webmvc (根据你已有的注释来判断)。 不过还有一件事情,那就是文件上传只适用于POST请求而不是其他任何东西,所以PUT将无法工作。 You are making it way to complex, to enable file uploading simply configure it correctly u ...
-
我最终在解决了这个问题后解决了这个问题,并且/或者避免了这个问题,所以我可能已经做了几件事来解决这个问题,但我相信它就像删除对super.configure(http);的调用一样简单super.configure(http); 。 这将设置基本身份验证。 韦斯。 I eventually solved this after working around and/or avoiding the problem so I may have done several things to solve the is ...
-
您的@GetMapping注释被忽略,因为在MarketDataService类的顶部没有@Controller类型的注释。 在你的情况下,你应该使用@RestController (它本身用@Controller和@ResponseBody注释): @RestController public class MarketDataService { @Autowired private MarketDataRepository marketDataRepository; @GetM ...
-
JBOSS EAP 6.2.4上的Spring Boot REST API部署(Spring Boot REST API deployment on JBOSS EAP 6.2.4)[2022-05-21]
我添加了以下onStartup并解决了这个问题 @Override public void onStartup(ServletContext container) throws ServletException { WebApplicationContext context = getContext(); ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherSe ...