简体   繁体   中英

Multithreaded application in Java?

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:

  1. Start multiple threads
  2. Get a list of all running threads
  3. Stop a given thread by name
  4. Stop all threads

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 .

Sun自己的主题教程怎么样?

The Executor interface provides a lot of useful methods for managing and executing threads, I would suggest taking a look at that. There's also an Executors class which provides different thread pooling options, and there's some good information here .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM