Java threads portal 8-6-14
2015-05-05Java 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
- notes from article
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)
- -example code I made from article that can be used as a template
- --Multiple thread boolean array counter (this code seems to work well and would work for a lot of different situations I think)
- ---"C:\Users\kurtw_000\Documents\kurt\storage\DR\2014\08-09-2014d0948\MultipleThreadBooleanArrayCounter"
- --Threads project with synchronization. This project was not working as expected . . I guess something would need to be fixed for it to work right
- ---"C:\Users\kurtw_000\Documents\kurt\storage\DR\2014\08-09-2014d0948\Threads"