Java threads portal 8-6-14

2015-05-05

Java threads portal 8-6-14
making java code run in parallel

Be careful with multiple threads. If you run too many threads (more than the computer can handle well) for too long, this seems to damage programs and the operating system.

This might be a good article about using multiple threads in Java.
http://www.fbi.h-da.de/~a.schuette/Vorlesungen/ParallelProgramming/Skript/JavaThreads/JavaThreads.pdf

How do I find out how many threads are being used?
use this command: Thread.activeCount()

When creating more than one thread, it is not possible to determine the execution sequence

Be careful not to let use threads use the same resources without protection

Java methods can be designated as "synchronized" to avoid lost information problems and active waiting problems. It looks like the "synchronized" method is more for situations when different threads are using the same resources.

A thread terminates when its run method has terminated, or in case of the "master" thread, when the main method has terminated.

You can check if a thread is alive
t.isAlive()
to do active waiting, but you should not do active waiting. The join function should be used instead.


It looks like "class Service implements Runnable" is great template code for starting a bunch of threads and then joining their results together later before moving to the next stage of the program.

use notify function to avoid active waiting

you can always use notifyAll instead of notify, but the reverse is not true

threads can be assigned priority values

there are user threads (running in the foreground) and daemon threads (running in the background)