core-async

Jakub Holý (HolyJak) 2023-01-26T19:36:58.933519Z

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)...

1
phronmophobic 2023-01-27T19:58:47.660629Z

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) 2023-01-27T20:10:28.404449Z

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.

👍 1
phronmophobic 2023-01-27T20:12:51.616919Z

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.

phronmophobic 2023-01-27T20:14:42.507529Z

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.

phronmophobic 2023-01-27T20:16:19.739559Z

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

👍 1