News for hackers. Stuff that matters.
Re:Things you didn't know about multithreaded programming by Jabbles on Tuesday November 09, 2010 @08:12AM (#1886286)
...in Java. Or at least, the JVM.

Does anyone know the limits of the JVM for multithreading? e.g. Max number of cores

Re:Things you didn't know about multithreaded programming by mzl on Tuesday November 09, 2010 @08:56AM (#1886387)
Any limits depend on the particular JVM that you are using. In practice one needs to experiment.

A few years back I did some comparisons between Java and Erlang with respect to the number of threads the system could handle. Somewhere around a thousand threads was sufficient to make the JVM go very slowly.

Re:Things you didn't know about multithreaded programming by lemming on Tuesday November 09, 2010 @09:40AM (#1886530)
See Kilim (http://www.malhar.net/sriram/kilim) for a lightweight thread solution for Java that easily outpaces Erlang for thread creation and message passing. It's the basis for the Erjang project which runs Erlang bytecode on the JVM. While it's faster at a low level, it obviously doesn't provide everything that Erlang does (supervisor hierarchies etc). It's still pretty impressive that this can be achieved on the JVM though.
Re:Things you didn't know about multithreaded programming by silentbicycle on Tuesday November 09, 2010 @11:03AM (#1886834)
Well, sure. Erlang processes aren't threads. They're more like pre-empted coroutines, with a scheduler that factors in when they're blocking and waiting to receive a message (among many other things).
Re:Things you didn't know about multithreaded programming by mzl on Tuesday November 09, 2010 @05:04PM (#1888128)
What would you say the essential difference is betwee pre-empted coroutines with a scheduler and threads?

The only thing I could think of is that with coroutines one can explicitly pass control over to another coroutine, but that is something that Erlang does not provide, so I don't really understand what you mean.

Re:Things you didn't know about multithreaded programming by silentbicycle on Tuesday November 09, 2010 @05:13PM (#1888155)
Erlang processes are managed by its scheduler, while threads are managed by the OS. Erlang has more info about appropriate stack sizes, garbage collection is done per-process* , it only pre-empts processes that are currently working or recently got a new message, and probably a half dozen other optimizations I'm not aware of. Let me know if you have more questions, I really like Erlang and I've wanted an excuse to research this. :)

* And often doesn't need to be done at all, because the process terminates first.

Re:Things you didn't know about multithreaded programming by mzl on Tuesday November 09, 2010 @05:06PM (#1888133)
Cool, I hadn't seen this before. I will definetly check it out. Thanks for the pointer.
Re:Things you didn't know about multithreaded programming by Jabbles on Tuesday November 09, 2010 @09:09AM (#1886421)
Surely the number of threads you can handle will depend on your hardware specs, in particular your cache sizes?

Also, were these threads proper OS threads, or more lightweight coroutines?

Thanks

Re:Things you didn't know about multithreaded programming by mzl on Tuesday November 09, 2010 @09:34AM (#1886512)
In my case it was about trying to make Java threads work in a situation where pre-emptive scheduling was needed for very many threads. The problem with the JVMs I tried was that they all started too many OS threads, and I couldn't get them to limit this enough. I might have done something wrong though. My general feel is that the threading model is too underspecified in Java.

I haven't been doing Java for a while now, but I hope that the situation has become better. The Erlang model of starting threads for all the tasks to do is very alluring once one gets used to it :)

Re:Things you didn't know about multithreaded programming by lemming on Tuesday November 09, 2010 @11:07AM (#1886848)
It may be very alluring but it's not going to work on Java since the model is different, and if you try to emulate Erlang on Java you're in for at least one surprise. This doesn't mean it's underspecified, in fact Java has arguably the most completely specified concurrency and memory model around. You just have to understand how it works to get the most out of it, like anything.
Re:Things you didn't know about multithreaded programming by mzl on Tuesday November 09, 2010 @05:14PM (#1888160)
The memory model is nicely specified, given that the program is race-free. If it isn't, then all bets are off. There are a lot of programs that are not race-free (although he races in themselves might be benign), and as such have a very weak memory model.

The concurrency model does have a very weak specification. It essentially says that a) threads exist, and b) threads with the highest priority will run at some point in time. This specification was left purposefully vague, AFAIK, so that a JVM implementation can rely on OS threads. Unfortunately, it also means that the programmer has to be aware of the specifics of all relevant JVM+OS combinations where a particular program is run. This is what I meant with underspecified, although I understand the reason for it.

Re:Things you didn't know about multithreaded programming by lemming on Tuesday November 09, 2010 @07:38PM (#1888652)
This is absolutely not true. If your program is not race free, then you're going to have problems no matter what your concurrency model is. The memory model is not a property of the program, it's a property of the platform - since in Java you're programming to the JVM platform, the fact that the platform is very exactly specified is what allows you to show that your program is race free.

I think you're looking for a very different sort of specification than what the JMM provides. It doesn't provide you with exact specifications of exactly how threads will behave, but no system that I'm aware of completely specifies that and any program that relies on that is fundamentally broken on any platform except exactly one version of one platform anyway - that which it was written and tested on. It's simply too dependent on the exact OS and version, the exact version of the processor, and a million other factors you can't control.

Instead, the JVM, the JMM and company work together to exactly specify the interactions between threads that are safe. It turns out that this really is all you need to know to write correct concurrent programs, and you don't have to worry about combinations of anything, or how threads are implemented, or how many processors are available, etc etc. Additionally, with the j.u.c libraries in Java 5+ there are sufficiently good abstractions that you don't need to worry about any of this low level gore much.

Seriously, if you're going to be writing multithreaded code in Java, read the book. It's awesome.

Re:Things you didn't know about multithreaded programming by motxilo on Wednesday November 10, 2010 @10:19AM (#1890531)
No, the JMM specification is not vague. Abridged, it provides a minimum set of guarantees in terms of data visibility between threads -happens-before rules- that the application programmer can expect. Think of it as an abstraction layer that portrays an uniform view of the myriad of hardware configurations on which the JVM runs -e.g. cache coherence strategies-, thus relieving the programmer from the burden of knowing the nuts and bolts of the target system for which its code is intended.
Re:Things you didn't know about multithreaded programming by davidw on Tuesday November 09, 2010 @09:20AM (#1886467)
> > comparisons between Java and Erlang with respect to the number of threads the system could handle

> Also, were these threads proper OS threads, or more lighweight coroutines?

Exactly, it's apples to oranges in terms of what's happening underneath. Erlang has its own scheduler, whereas Java threads are system threads.

Re:Things you didn't know about multithreaded programming by mzl on Tuesday November 09, 2010 @09:35AM (#1886516)
> Java threads are system threads

AFAIK, Java threads are not required to map directly onto system threads.

Re:Things you didn't know about multithreaded programming by davidw on Tuesday November 09, 2010 @09:38AM (#1886521)
Oh - I wasn't aware of that. Are there any of the main implementations where they aren't?
Re:Things you didn't know about multithreaded programming by mzl on Tuesday November 09, 2010 @10:03AM (#1886613)
It is possible to manage it manually using Executors. Take a look at thread pools in http://download.oracle.com/javase/tutorial/essential/concurr... for an overview look at what to do.

I hope that it works better than the last time I tried, since I will probably have to write some multi-threaded Java code quite soon :)

Re:Things you didn't know about multithreaded programming by lemming on Tuesday November 09, 2010 @10:27AM (#1886686)
This is a little misleading. It may be true that the Java specification doesn't require Java threads to map onto system threads but for all intents and purposes they're the same in all real implementations. The Executor framework doesn't change this, it gives you a flexible framework for task scheduling using thread pools.
Re:Things you didn't know about multithreaded programming by mzl on Tuesday November 09, 2010 @05:02PM (#1888119)
A lot of my earlier work was in formal specification of JVM thread behaviour, so I was very interested in what the standard said and required. I agree that in practice, it probably will map to threads, which is unfortunate. The experiments were mostly to satisfy my curiosity on how stuff behaved in the real world.
Re:Things you didn't know about multithreaded programming by wmf on Tuesday November 09, 2010 @04:36PM (#1888018)
Back in the dark ages the Sun JVM used green threads. More recently, JRockit added "thin threads" to win VolanoMark. http://download.oracle.com/docs/cd/E13222_01/wls/docs81b/jro...
Re:Things you didn't know about multithreaded programming by tomjen3 on Tuesday November 09, 2010 @08:18AM (#1886297)
properly 2^32-1.