griffin
griffin

Reputation: 3234

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?

Upvotes: 1

Views: 825

Answers (4)

Stephen C
Stephen C

Reputation: 718778

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

Upvotes: 1

Pascal Thivent
Pascal Thivent

Reputation: 570325

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.

Upvotes: 4

Kaleb Brasee
Kaleb Brasee

Reputation: 51925

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.

Upvotes: 3

BalusC
BalusC

Reputation: 1108702

How about Sun's own tutorial on the subject?

Upvotes: 3

Related Questions