views:

168

answers:

4

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?

+3  A: 

How about Sun's own tutorial on the subject?

BalusC
+3  A: 

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.

Kaleb Brasee
Afaik, the Executors have no way to control threads by name. Once a Task or Runnable is submitted, there is no control any more.
mhaller
+4  A: 

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.

Pascal Thivent
+1  A: 

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 ...

Stephen C