Fork me on GitHub
#core-async
<
2023-01-26
>
Jakub Holý (HolyJak)19:01:58

Hello! If I read the docs correctly then there is no way to cancel the operation inside a/thread, eg after a timeout, correct? I'd need to impl myself inside it's body (eg checking a channel for being closed)...

yes 2
phronmophobic19:01:47

I don't believe a non-cooperative method exists, either on the JVM or in javascript land. In other words, the code running in your thread must provide support for interruption. However, if you're running in a thread on the jvm, you can also use .interrupt (https://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html). Thread .interrupt is supported by some of the java standard library. It can be tricky to get right, but sometimes, it's the right tool.

Jakub Holý (HolyJak)20:01:28

Right, thank you. But if I use a/thread then I only have a channel, not the thread. I guess inside its body at the very start I could use currentThread and store it somewhere so that an external process can .interrupt it. Likely do it in try … finally and remove it from that place in the finally block so that it is not interrupted when it already is used for other purposes then the a/thread.

👍 2
phronmophobic20:01:51

I guess trying to interrupt a thread created by a/thread is a bad idea since threads come from a cached thread pool and the thread may have already moved onto another go block.

phronmophobic20:01:42

Depending on if you want to use .interrupt, you could also use your own thread pool that helps support this use case. Using an "interrupt channel" is also a good option in many cases.

phronmophobic20:01:19

Cancelling and interrupting code is always tricky. Maybe the jvm's loom project will help in the future.

👍 2