Set Java call stack
Asked Answered
P

1

0

is it possible to modify/access the Java call stack ? For instance saving and restoring ? I'm thinking about implementing cooperative multitaskting (especially I/O) just like gevent for python or haskell (which does this natively). The async callback spaghetti mess can't be the best solution.

Potvaliant answered 31/7, 2013 at 16:28 Comment(0)
A
0

The simplest solution is to have multiple threads. You can have up to 10,000 threads running efficiently on a server. If you need much more than this, I would suggest either a) buying a second server, or b) using non-blocking IO.

In general, using multiple threads is the simplest and for small numbers of threads, is the fastest too.

There are libraries to do this in Java in various manners. They all suffer from a common problem, they are either slower, or much more complicated or both.

BTW I make this point because when a thread context switch it does exactly what you suggest, but the OS does it for you.

Argyll answered 31/7, 2013 at 16:51 Comment(4)
10,000 would be pretty nice. But I thought that such a huch amount of threads would mean tons of context switches which in turn decrease the performance massively. This gets even worse when the threads need to communicate. Lightweight threads are nice because there is no context switch which would involve kernel lock etc. Practically its pretty simple if Java would let me access the stack programmatically. I like the concept of cheap native OS threads but I don't think this is possible. EDIT: The lightweight threads could then be managed using async IO.Potvaliant
Java threads aren't necessarily kernel threads. The JVM is free to implement the threading however it wants, and thread pooling is common. Modifying the live call stack is basically the same thing as rewriting the VM, and there's no way to do that from within Java itself. A VM agent could do so, but you're much better off using the debugged and tuned threads implementation you already have available.Denicedenie
I'm not aware of a JVM which does not use kernel threads beside one of the first JVMs (1.1). But they changed quite fast to kernel threads. The possibility that at some time JVM implementations will support lightweight threads is therefore quite unlikely. For network applications green threads are the way you go. Unfortunately only a few languages does really support green threads like Haskell, Go and C# (C# kinda fakes them, not as beautiful as Haskell or Go), Python using Gevent. Well I guess I have to go with a billion lines of callback spaghetti code =(Potvaliant
There are libraries which support continuations, but these are slower and are not uses for performance (they can be useful for other reasons) Before you do down the path of complicating your code,I would check your really need to do this because you might find it is a complete waste of your time doing this. I would let the OS threads do what they are designed to do unless you know this is a problem, not based on a feeling it would be nice not to have to do this.Argyll

© 2022 - 2024 — McMap. All rights reserved.