I haven't worked with Java threads in several years so I have a very basic question about multithreading. I'm writing a java package that will be called by another language (matlab). Matlab is able to instantiate a Java class and run java code.
I want to be able to:
I used the Thread
class in the past, but are there any easier/more complete packages available now? Can anyone provide a simple demo or point me to a tutorial on the subject?
Maybe have a look at the examples in the online supplement of Concurrent Programming in Java: Design Principles and Patterns by Doug Lea which is the book on Threads and concurrent programming (but it doesn't cover the new java.util.concurrent
package).
Or check the more recent Java Concurrency in Practice by Brian Goetz, Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, Doug Lea (a dream team for such a book) and its companion website .
Warning: if you need to be able to stop threads (safely), you need to code them so that they respond correctly to Thread.interrupt()
. For example, a compute intensive thread needs to check the interrupted
flag occasionally, and IO requests need to be done using APIs that allow a blocking call to be interrupted. This is not simple ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.